Jump to content
  • Advertisement
Sign in to follow this  
darkchrono4

[java] Resizing arrays

This topic is 4869 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

Another thing I have to do is remove rows/columns from a matrix. The removing it part isn't that big of a deal. I have a private array that points to the matrix that was opened up. I can resize it and say assign '0' to all the elements fine. But if I have another array with the new data and try to assign it to the private array I get some kind of null exception error when I try to save the array to a text file.

Share this post


Link to post
Share on other sites
Advertisement

Integer temp[][] = new Integer[3][5];
int x, y;


private_matrix = new Integer[3][5];
for (x=0; x<3; x++) {

for (y=0; y<5; y++) {
temp[x][y] = private_matrix[x][y];
}
}

for (x=0; x<3; x++) {
for (y=0; y<5; y++) {
private_matrix[x][y] = temp[0][0];
}

}


I realize it doesn't actually do anything. But in the second loop when private_matrix is assigned the values of temp is where the problem seems to be. If I just assign say '0' I don't get an error.

private_matrix was initilized elsewhere as a 4x5 array loaded in with values from a file

Share this post


Link to post
Share on other sites
When you do "private_matrix = new Integer[3][5]", you're nulling all indicies in private_matrix.

The reason for this is because you're using the Integer object (java.lang.Integer) rather than the Integer primitive (int).

Your null errors are probably because of this.

Share this post


Link to post
Share on other sites
There is a mistake that assignment should read "private_matrix[x][y] = temp[x][y];".

If the array has been resized and all the elements should have values in them why would I be getting null?

I'm using the Integer class because I need to use .toString() when I'm saving it.

Share this post


Link to post
Share on other sites

Integer temp[][] = new Integer[3][5]; //Every temp index is null now.

int x, y;

private_matrix = new Integer[3][5]; // Every private_matrix index is null now.

for (x=0; x<3; x++){
for (y=0; y<5; y++){
temp[x][y] = private_matrix[x][y]; // null to null
}
}

for (x=0; x<3; x++){
for (y=0; y<5; y++){
private_matrix[x][y] = temp[0][0]; // null back to null
}
}



Everything in Java is stored in reference. When you resize an object array, all references in that array are set to null. Similarly, when you resize a primitive array, all references get set to that primitive's default value.

Right now you're swapping null references.

If I understand what you want to do correctly, you need to copy the data from "private_matrix" into "temp" prior to resizing it. Then you can resize "private_matrix" and dump the data in "temp" back into it.

Share this post


Link to post
Share on other sites
Your right, I was being stupid and resizing the array before copying it to the temp array. If I wait to resize it until after I've copied what I need to the temp array I don't get the errors.

Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
I'm using the Integer class because I need to use .toString() when I'm saving it.


As a general rule, use primitives in Java in preference to their object counterparts, when you can get away with it; use the objects when you have to (e.g. as keys for a HashMap).

To convert a primitive integer to a String, you can either invoke the static Integer method (like Integer.toString(myIntValue)), or the static String method for the task (like String.valueOf(myIntValue)), or do it implicitly via a string concatenation (like "" + myIntValue, or myStringBuffer.append(myIntValue)).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!