Sign in to follow this  

[java] Changing Array Size

This topic is 4159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; else attackable = 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 executed

attackable = 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 memory
Tile[] someOtherOtherTileArray = new Tile[5]; //allocate memory for 5 Tiles and set someOtherOtherTileArray to point to that memory
Tile[] 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 array
int newArray = new int[200]; //declare and allocate the array with a new size
for(int i=0; i<array.length(); i++)
newArray[i] = array[i]; //copy over stuff from the old array to the new array
array = newArray; //set the reference so that the old array points to the new array
array.length(); //at this point this should be 200, not 100




Share this post


Link to post
Share on other sites
Quote:
Original post by deathkrush


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 array
int newArray = new int[200]; //declare and allocate the array with a new size
for(int i=0; i<array.length(); i++)
newArray[i] = array[i]; //copy over stuff from the old array to the new array
array = newArray; //set the reference so that the old array points to the new array
array.length(); //at this point this should be 200, not 100


This code works, but there is a cleaner way.

int[] array = new int[100]; //declare and allocate the original array
int newArray = new int[200]; //declare and allocate the array with a new size
System.arraycopy(array,0,newArray,0,array.length);
array=newArray;


Share this post


Link to post
Share on other sites

This topic is 4159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this