-
Array with objects
Hi, I have made myself a class like follows
Code:
class State {
static int[][] grid = new int[2][2];
static int parent;
static int depth;
}
And would like many instances of this class. I tried
Code:
State[] states = new State[1000];
But it does not set each instance correctly when I try states[0].grid = .... It treats it like one instance. What is the correct way to do this?
Thanks.
-
the reason is that u r using "static" access for grid.
wen a variable is defined static it means that it is defined not for one instance of the class instead it is unversal for all the instances the class has.
so if i say
static int i=0; then all the instances of class will have i=0;
and ny changes to i will affect the value of i in all instances.
so the solution is to use public or private access for these variables instead of static
so the class shld look like......
class State {
public int[][] grid = new int[2][2];
public int parent;
public int depth;
}
state[] obj=new state[1000]
obj[0].grid=.............