I have an object with an array as parameter and I'm trying to copy that object in a way I can modify one without modifying the other. Here's my class:
public class Matrix {
int rows[];
Matrix() {
rows = new int[9];
}
int[] getRows() {
return rows;
}
void setRow(int x, int y) {
rows[x] = y;
}
int getRow(int x) {
return rows[x];
}
}
I want to do something like:
(Consider Object Matriz x is all filled with values)
Matrix k = new Matrix();
k = x;
(Now I want to modify a specific column without of k without modifying a column of x)
k.setRow(3, 3);
But what happens is I get both arrays as same because of the reference when I do k = x;
How can I avoid this and duplicate them instead of creating references? I would like to avoid copying cell by cell to avoid an increase of the run time. (One solution is creating a primitive class Matriz with a sub-class Cell with int values, but besides that?)
Thanks