简体   繁体   中英

Initilizing a static array within an object class in Java

I would like to have a static array in a Java class with an undetermined size initially. The intent is to use the array to store values calculated in a method in one class and used in another.

Eg the 'TwoDX(Y)Pos[] arrays defined here:

public class DisplayObject {

    //  Each object is defined by a color, number of vertices, and dim (2,3,4) coordinate vertex locations
    //  Use dim to verify that the right number of vertices were sent

        public static int Dim=3;
        public static int RefDist=100;

        public static int TwoDXpos[];
        public static int TwoDYpos[];
}

And used here:

    public void render2D(){

        for (int cnt=0; cnt<this.NoOfVerts; cnt++){
            if (this.coords[cnt*Dim+2] < RefDist) {break;}
            TwoDXpos[cnt]=this.coords[cnt*Dim]/(this.coords[cnt*Dim+2]/RefDist);
            TwoDYpos[cnt]=this.coords[cnt*Dim+1]/(this.coords[cnt*Dim+2]/RefDist);
        }
    }

But, since the original static references have no defined size, they reference Null pointers at execution.

How would you create such arrays?

I would like to have a static array in a java class with an initially undetermined size.

Sorry.

That isn't possible in Java. JLS-10.3. Array Creation says (in part)

The array's length is available as a final instance variable length .

Alternative

However, you could have a List of Foo (s) like

List<Foo> al = new ArrayList<>();

Use ArrayList instead of arrays. Your code should look like this:

public class DisplayObject {

//  Each object is defined by a color, number of vertices, and dim (2,3,4) coordinate vertex locations
//  Use dim to verify that the right number of vertices were sent

    public static int Dim=3;
    public static int RefDist=100;

    public static ArrayList<Integer> TwoDXPos;
    public static ArrayList<Integer> TwoDYPos;
}

and the render 2d method:

public void render2D(){

    for (int cnt=0; cnt<this.NoOfVerts; cnt++){
        if (this.coords[cnt*Dim+2] < RefDist) {break;}
        TwoDXpos.get(cnt)=this.coords[cnt*Dim]/(this.coords[cnt*Dim+2]/RefDist);
        TwoDYpos.get(cnt)=this.coords[cnt*Dim+1]/(this.coords[cnt*Dim+2]/RefDist);
    }
}

The advantage of using ArrayList is that you can use its add(item) method to change its size dynamically.

Hope it helps!

 public void render2D() {
    TwoDXpos = new int[this.NoOfVerts];
    TwoDYpos = new int[this.NoOfVerts];
    for (int cnt = 0; cnt < this.NoOfVerts; cnt++) {
        if (this.coords[cnt * Dim + 2] < RefDist) {
            break;
        }
        TwoDXpos[cnt] = this.coords[cnt * Dim] / (this.coords[cnt * Dim + 2] / RefDist);
        TwoDYpos[cnt] = this.coords[cnt * Dim + 1] / (this.coords[cnt * Dim + 2] / RefDist);
    }
 }

and pls following Java naming rules to name your variable: http://www.iwombat.com/standards/JavaStyleGuide.html#Attribute%20and%20Local%20Variable%20Names

Short answer is you need to initialize the array before using them. To avoid memory leakage, you should set its size at initialization. eg10.

public static int TwoDXpos[] = new int[10]; public static int TwoDYpos[] = new int[10];

If the array size changes, you should use ArrayList because its size is automatically managed by JVM

You can't use an array whose size is not determined. You should initialize the array size before method render2D() called. May be you could use this.NoOfVerts as the array size.

If you are going to use an array, you should initialize it by mentioning the size of the array in order to allocate memory before using it.

public static int TwoDXpos[];
public static int TwoDYpos[];

Any access on this would throw NullPointerException because the memory for this not allocated and object defintion has not happened at all.

You ought to remember this on array (or any objects for that sake) "Initialize before you access/use them"

If you worry is about that you are not sure about the number of elements upfront, you should use utilities like ArrayList which is provided in Java Collection Framework . This would take care dynamically adjusting the size as the number of elements increases.

Alternative Approach

private static List<Integer> twoDXPos = new ArrayList<Integer>();
private static List<Integer> twoDYPos = new ArrayList<Integer>();

and then itemes can be added using add method in java.util.Collection class. (Refer the link I gave above)

twoDXPos.add(1) //1 is an example integer here to illustrate
twoDYPos.add(1) //1 is an example integer here to illustrate

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