简体   繁体   中英

Unable to store object array length in Java

I'm learning basic Java and I'm having a little trouble with object arrays.

Some background on this code: I have a separate class called Point1, a simple point on a Cartesian plane, the properties of which that are relevant to this question being:

  • 2 private integer variables _x and _y which denote the point's corresponding coordinates
  • A constructor with arguments for _x and _y eg: Point1 a = new Point1(0,0)
  • Public methods for setting x and y values: a.setX(int x); a.setY(int y); a.setX(int x); a.setY(int y);

I'm now being asked to create a class which contains an instance of Point1 as an array and I'm having some trouble with it. When I use this code:

public class Polygon {
     private int _vertNum;
     private Point1[] _vertex = new Point1[_vertNum];

     public Polygon(int verts) {
         _vertNum = verts;

         for(int i = 0 ; i < verts ; i++) {
             _vertex[i] = new Point1(0,0);
         }
     }
 }

It compiles fine but when I try to create an instance of the class, I receive this message: java.lang.ArrayIndexOutOfBoundsException: 0

If I add an array declaration in the constructor, like this:

public class Polygon {
    private int _vertNum;
    private Point1[] _vertex = new Point1[_vertNum];

    public Polygon(int verts) {
        _vertNum = verts;
        Point1[] _vertex = new Point1[_vertNum];

        for(int i = 0 ; i < verts ; i++) {
            _vertex[i] = new Point1(0,0);
        }
    }
} 

I am able to create an instance of the class but upon inspection its length is always 0 regardless of what I've entered in the constructor parameter.

My goal is to create an array of points whose length is set by the constructor's parameters and, at least for now, set the x and y coordinates of all those points to 0,0 so I'm not left with an array of null objects.

I'm well aware this is probably a silly question and I apologize for the long post - I would ask my professor but he's a bit of an unreasonable fellow so I would really appreciate the help. Thanks in advance :)

Try this.

public class Polygon {
     private int _vertNum;
     private Point1[] _vertex;

     public Polygon(int verts) {
         _vertNum = verts;
         _vertex = new Point1[_vertNum];

         for(int i = 0 ; i < verts ; i++) {
             _vertex[i] = new Point1(0,0);
         }
     }
 }

In your original code, _vertNum was = 0 when _vertex was being initialized, and you ended up with a zero length array.

I'm learning basic Java and I'm having a little trouble with object arrays.

Don't think of them as objects array for now. Concentrate on the basic concepts of arrays. Arrays do need to have the size and they are fixed sized which means, once their size is defined, you can't modify that.

private int _vertNum;

private Point1[] _vertex = new Point1[_vertNum];

_vertNum is an instance variable, which means, that it will get the defaul value, so as its an int, it will get it's default as 0 . So, you initialize your Point1 array with size of 0.

Now, when you do this:

_vertex[i] = new Point1(0,0);

You are actually trying to assign the value to index 0 which doesn't even exist. So, the work around can be something like,

-> Initialize your instance variable with some size instead of default.

OR

-> Initialize your array inside the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM