private int[][] gameBoard;
private int[][] scratchBoard;

public Intboard(int[][] world)
{
  gameBoard = world;
  scratchBoard = world;
}

public void updateBoard()
{
  gameBoard = scratchBoard;
}

//...skipping a bunch of confirmed working methods...

public void advanceOneGen()
{
  for(int x = 1; x<gameBoard.length-1; x++)
  {
    for(int y = 1; y<gameBoard.length-1; y++)
    {
	if(gameBoard[x][y]!=2)
	{
	  if(valueOfNeighbors(x,y)<=1 || valueOfNeighbors(x,y)>=4)
	  {
            scratchBoard[x][y]=0;
	  }
	  if(valueOfNeighbors(x,y)==3 && gameBoard[x][y]==0)
	  {
	    scratchBoard[x][y]=1;
	  }
        }
      }
    updateBoard();
}	
}

My suspicion is that after the initializer, gameBoard[][], scratchBoard[][], and the (destroyed?) world[][] are all pointing to the same object. i'm thinking this because it seems to fit the problem (if advanceOneGen() was both reading from and modifying only one Board), but i could have sworn ints were immutable (and so a 2D array of them also should be, right?).

In case it might be relevant, this is for a Game of Life. the inital input is a cross, which i would expect to transform into a 3x3 box with an open center, but instead what's pictured.


here is a picture to be more clear:
http://img519.imageshack.us/img519/1150/38718066ff5.jpg

> but i could have sworn ints were immutable (and so a 2D array of them also should be,
> right?).
The concept of immutability doesn't apply to primitives. Sure, the objects of all the wrapper classes for primitives are immutable, but the primitives themselves aren't and neither are arrays by default.

In your case, the array references gameboard, scratchboard and world all point to the same array object in heap. In case you want to preserve the original, clone the array instead of just assigning references.

and even if ints were immutable, he's talking about arrays which certainly aren't (unless explicitly declared final of course).

public Intboard(int[][] world)
{
	gameBoard = world;
	scratchBoard = (int[][])world.clone();
}

public void updateBoard()
{
	gameBoard = (int[][])scratchBoard.clone();
}

I've updated the code, but am getting the same output. is my syntax for cloning wrong, or does int[][] not enable it, or is there something else i'm missing? do i have to put a ".super" in somewhere?

thats a shallow clone not a deep clone. A deep clone is where you'd iterate through and actually clone the object!

A shallow clone is ok if your object is a primitive; but there again why would you want to call clone on a primitive?

Does that make sence?

then what object do i reference to extend int[][]? java.lang.reflect.Array, the Array interface, or something else?

no just copy the array into another array and return the new array? bit like operator overloading?

no just copy the array into another array and return the new array? bit like operator overloading?

what?

This article has been dead for over six months. Start a new discussion instead.