-1

So. I have a problem I simply cant get my head around. I'm currently making a cellular automaton. (Java) For this, I have 2 Arrays, one called cells[][] for current states, and one called cellsX[][] for the temporary state inbetween steps.

at each update i do this:

public void updateCells() {
    cellsX = cells;
    for(int i = 0; i< Xsize; i++) {
        for(int j = 0; j < Ysize; j++) {
            cellsX[i][j].Update();
        }
    }   
}

And later I render:

for(int i = 0; i< Xsize; i++) {
    for(int j = 0; j < Ysize; j++) {
        if(cells[i][j].isAlive()) {
            int Colori[] = cells[i][j].GetColor();
            g2d.setTransform(identity);
            g2d.translate(0, 0);
            g2d.setColor(new Color(Colori[0],Colori[1],Colori[2]));
            g2d.drawRect((i*5)+20, (j*5)+20, 5, 5);
        }
    }
}

Right now, I would say nothing should happen, as I newer update cells[][] but for some reason it do? How can the cells[i][j] update, when the only cell I've given a command is cellsX[i][j]?

to show you the Update function in the Cell

public void Update() {
    if(Info[0][0] > 0) {
        Info[0][0] += 1;
        Info[0][2] += rand.nextInt(2);
    }
    Info[1][0] ++;
    if(Info[1][0] >255) Info[1][0] =0;
    if(Info[0][0] > Info[0][1]) Info[0][0] = 0;
    if(Info[0][2] <= 0) Info[0][0] = 0;
}

Its a void an no nothing to affect the outside world (Info[][] is an int array used to store data such as life and color (Info[1][0] is the red color)

I have no idea how the H. i can mess up. the creation of the cells and cellsX is

int Xsize = 100;
int Ysize = 100;
Cell[][] cells = new Cell[Xsize][Ysize];
Cell[][] cellsX = new Cell[Xsize][Ysize];

and initialized:

for(int i = 0; i< Xsize; i++) {
    for(int j = 0; j < Ysize; j++) {
        cellsX[i][j] = new Cell();
    }
}
for(int i = 0; i< Xsize; i++) {
    for(int j = 0; j < Ysize; j++) {
        cells[i][j] = new Cell();
    }
}

Sorry for the wall of text... I just can't figure out how cells get's updated :S

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • 1
    What is `cellsX = cells;` in `updateCells()` supposed to do? – zapl Dec 08 '13 at 01:40
  • How is this related to synchronization? – Vitruvie Dec 08 '13 at 01:41
  • zapl: making sure that cellsX is reset between each update. Saposhiente: the sync problem is that cells somehow syncs with cellsX after cellsX is updated, and it is not supposed to. – JavaApprentis Dec 08 '13 at 01:46
  • I don't think you understand that arrays are objects. `cellsX = cells` ... you now have exactly one array with two variables holding a reference to it. The one you originally created and assigned to `cellsX` has been thrown to the GC and is no more. – Brian Roach Dec 08 '13 at 01:55
  • How should this not give me 2 2d arrays, one called cells and another called cells X, just above I call 2 ints called Xsize and Ysize, they are not the same... if not, how am i supposed to make 2 separate arrays of the class Cell? – JavaApprentis Dec 08 '13 at 02:00

2 Answers2

2

From your comment -

"How should this not give me 2 2d arrays, one called cells and another called cells X"

You do it's just that they equal each other here - cellsX = cells; Consider, and run/watch, this simple example...

public static void main(String[] args){
    int size = 5;
    int foo[][] = new int[size][size];
    int bar[][] = new int[size][size];

    for(int i = 0; i < size; ++i) {
        for(int j =0; j < size; ++j) {
            System.out.print(foo[i][j] + " ");
        }
    }
    System.out.println();
    foo = bar;
    for(int i = 0; i < size; ++i) {
        for(int j = 0; j < size; ++j) {
            bar[i][j] = j;
        }
    }
    for(int i = 0; i < size; ++i) {
        for(int j =0; j < size; ++j) {
            System.out.print(foo[i][j] + " ");
        }
    }
}//SSCCE1

Output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4

This shows that while I do have two 2D arrays they equal each other, they are not unique copies of each other. foo was a 2D array that was initialized by default to 0, but then we have foo reference bar. So the changes made to bar are reflected by foo. You want, I'm supposing, an independent copy initialized with the values of the other.

See that Arrays are Objects.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
1

What you don't seem to understand is what the following line in your code does:

cellsX = cells;

Arrays in Java are objects. Variables hold references to objects. You just assigned the reference held in cells to cellsX. You now have two variables pointing to one array object. The array (and the arrays it contained) you originally instantiated and assigned to cellsX is gone; nothing references it and it will be garbage collected.

Consider the following:

public class Demo {

    public static class MyPojo {
        public int value;
        public MyPojo(int value) {
            this.value = value;
        }
    }

    public static void main(String[] args) {

        MyPojo pojoOne = new MyPojo(1);
        MyPojo pojoTwo = new MyPojo(2);

        System.out.println(pojoOne.value);
        System.out.println(pojoTwo.value);

        pojoTwo = pojoOne; 
        // You now have two variables holding a reference to 
        // a single instance of MyPojo. Changes made through
        // either affect the same object. The object you 
        // originally instantiated and assigned to pojoTwo 
        // is no longer referenced by anything and will be 
        // garbage collected. 
        pojoOne.value = 3;
        System.out.println(pojoTwo.value); 

    }
}

If in your code you're trying to copy the contents of the array referenced by cells to the array referenced by cellsX you would need to do so.

Since this is a multi-dimensional array you need to do a deep-copy; What you really have is an array of array references (since, again, arrays are objects in Java). This StackOverflow question covers that.

Community
  • 1
  • 1
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • Thanks, that did make sense. I did not know that.. I solved the problem by looping through the array and copy the data piece by piece. this works fine, and is no problem in this for this program, but is there a faster way? – JavaApprentis Dec 09 '13 at 23:57
  • 1
    Nope. That's what you need to do. – Brian Roach Dec 10 '13 at 00:10