开发者

Java Need to read in an unspecified number into an array

ok so i need to fill an array with integers based on a number that i specify in another part of the program. This is what I have so far:

public abstract class Polygon implements Shape {


      //numSides is the number of sides开发者_如何学Python in a polygon 

        int numSides; 

          //Vertices[] is the array of vertices, listed in counter-clockwise sense 

        Point[] Vertices; 
        public Polygon(Point[] Vertices){ 
                //THIS IS WHERE I NEED THE HELP, DONT KNOW WHAT TO PUT IN HERE  
            }


       public Point getPosition(){ 
           return this.Vertices[0]; 
                }

       }

Thank you in advanced.

Sorry for the lack of information. Yes this is for a class. I can see how an ArrayList would probably be better. The program its self has an interface, Shape, which gets implemented by a class Point and a class Polygon, which them has a class that extends that, rectangle. The idea is to put the number of vertices into an array and return the value at Vertices[0] as the position of the polygon. The rest of this class looks like this:

    public abstract class Polygon implements Shape {

       //numSides is the number of sides in a polygon 

        int numSides; 
        Point[] Vertices; 
        public Polygon(Point[] Vertices){ 
            //your code
            }


        public Point getPosition(){ 
          return this.Vertices[0];  
         } 
        }

Not sure if you need to see the rest of the program. thank you again.


You are not being very clear in what you need, but I guess you want help on your constructor:

public Polygon(Point[] Vertices){ 
    this.Vertices = Vertices;
    this.numSides = Vertices.length;
}


Maybe this will guide you in the right direction:

public static void main(String[] args) {
    System.out.print("how many? ");
    Scanner in = new Scanner(System.in);
    int size = in.nextInt();
    int[] numbers = new int[size];
    System.out.println("Enter the " + size + " numbers.");
    for (int i = 0; i < size; i++) {
        System.out.print(" " + (i + 1) + ": ");
        numbers[i] = in.nextInt();
    }
    System.out.println("Numbers entered: ");
    for (int number : numbers) {
        System.out.print(number);
        System.out.print(' ');
    }
}

As the others have said, more detail in the question will bring better detail in answers. Also, this smells like homework. If so, you should tag it as such.


As your question is not clear I am assuming that you that you just want to fill your array with random integers.
Here first of all you need to specify size of the array, and the best place to do this is your constructor.

 public Polygon(Point[] Vertices){ 
        Vertices = new Point[i];
       //based on a number i specified in another part of the program
       // Now you can use the scanner class to fill your array, see Ryan Stewart's answer for details on using Scanner class.
    }

I hope this helps :)
cheers!!!


I think, you need an elegant array copy:

public Polygon(Point[] Vertices)
{ 
    if (Vertices == null || Vertices.length == 0)
    {
        return;
    }

    int i = Vertices.length;
    this.Vertices = new Point[i];
    System.arraycopy(Vertices, 0, this.Vertices, 0, i);
}

After, you can iterate over your array:

    ...
    for (Point point : this.Vertices)
    {
        // Use point
    }
    ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜