   [java] Changing Array Size
  Hey everyone. I've got a problem that I can't seem to figure out.I have a class called Tile, and I initiliaze an array of Tiles in a method, but I'm not sure what the size of the array should be until I do two if statements. So it looks something like this:Tile[] attackable;if (x > 5)attackable = someOtherTileArray;elseattackable = someOtherOtherTileArray;the problem is it seems like the attackable array keeps on being set to null, even when I set it to some other tile array. My guess is this is because its size is null. I can't initially set it to one of the other tile arrays because there's a chance it might be the other array, and since they may be different sizes I could run into the same problem.Is there some way I could change the size of the array depending on the if statement? Or is there some other method I could use to get this working?Thanks.   
  Maybe someOtherTileArray or someOtherOtherTileArray are null?   ;  I don't think that's it, because when I do this everything works:Tile[] attackable = someOtherOtherTileArray;if (x > 5)attackable = someOtherTileArray;unless x happens to be greater than 5, in which case it gives me errors again. I think that's because the program is looking for something in attackable, that is in someOtherTileArray. I think since someOtherTileArray might be bigger than someOtherOtherTileArray, then attackable isn't taking in the whole array and the piece that's left out is what is needed. Is that possible, and if so is there a way around it?   ;  In Java when you declare an array like Tile[] attackable; Java just creates a null reference that doesn't contain any actual data. What the assignment operator does is simply set that reference to point to a location in memory (kind of like pointers in C++). Anyway, when this line gets executedattackable = someOtherTileArray;attackable now points to the same array that someOtherTileArray points to. How can attackable become null? Only if someOtherTileArray is null. Notice that Java doesn't copy any data from arrays, it just sets the references, so it doesn't care about array sizes.How else can a pointer become null? Consider this case:Tile[] someOtherTileArray = new Tile[10]; //allocate memory for 10 Tiles and set someOtherTileArray to point to that memoryTile[] someOtherOtherTileArray = new Tile[5]; //allocate memory for 5 Tiles and set someOtherOtherTileArray to point to that memoryTile[] attackable = null; //declare a reference and make it point to null (no memory allocation)if (x > 5)attackable = someOtherTileArray; //make attackable point to the same memory that someOtherTileArray is pointing to else if(x < 4)attackable = someOtherOtherTileArray; //make attackable point to the same memory that someOtherOtherTileArray is pointing to In this case if x==4, both if conditions won't be met and attackable will be null!As you can see, the only way to have null reference in Java after assigning it to a valid array is by making a logic mistake.EDIT: To change the size of an array in Java you can allocate a new array with the new size, copy the old array into it and make the old array point to the new array. Like this:int[] array = new int[100]; //declare and allocate the original arrayint newArray = new int[200]; //declare and allocate the array with a new sizefor(int i=0; i<array.length(); i++)    newArray = array; //copy over stuff from the old array to the new arrayarray = newArray; //set the reference so that the old array points to the new arrayarray.length(); //at this point this should be 200, not 100;Quote:Original post by deathkrushEDIT: To change the size of an array in Java you can allocate a new array with the new size, copy the old array into it and make the old array point to the new array. Like this:int[] array = new int[100]; //declare and allocate the original arrayint newArray = new int[200]; //declare and allocate the array with a new sizefor(int i=0; i<array.length(); i++)    newArray = array; //copy over stuff from the old array to the new arrayarray = newArray; //set the reference so that the old array points to the new arrayarray.length(); //at this point this should be 200, not 100This code works, but there is a cleaner way.int[] array = new int[100]; //declare and allocate the original arrayint newArray = new int[200]; //declare and allocate the array with a new sizeSystem.arraycopy(array,0,newArray,0,array.length);array=newArray;