[java] Resizing arrays
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.
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
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.
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.
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.
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.
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.
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.
Thanks for the help.
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)).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement