2

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

2
  • You want two independent copies of the data, but you don't want to copy the data? How is that supposed to work? You won't get around implementing some kind of deep-copy for your class. Commented Mar 16, 2015 at 10:49
  • I just want to be able to have a matrix, and create another matrix equal to the first, but to be able to modify it without modifying the first one. It's a clone and this is basic stuff that has to be possible.. Commented Mar 16, 2015 at 10:51

3 Answers 3

2

First of all, you call your class Matrix, but it's not a matrix. It only has one dimension, so Vector would be more accurate.

In order to duplicate your instance, you can use a copy constructor.

Matrix(Matrix other) {
    rows = Arrays.copyOf(other.rows, other.rows.length);
}

You create a copy of x by :

Matrix k = new Matrix(x);
Sign up to request clarification or add additional context in comments.

4 Comments

What if I had a multidimensional matrix, let's say a bidimensional. Do I have to use Arrays.copy the number of times as the lines of the matrix?
@Eran I must be blind or something
@PedroBarros If your member was a multi-dimentional array, you'll have to create a copy of it by yourself, using a nested for loop. copyOf only handles 1D arrays.
Baaah, I guess I just have to accept it's going to be slow with multidimensional and there's no workaround :p
1

You could make a second constructer that takes another Matrix as parameter and copys it.

public Matrix(Matrix matrix) {
   int[] matrixRows = m.getRows();

   this.rows = new int[matrixRows.length];
   for(int i = 0; i < matrixRows.length; i++) {
       this.rows[i] = matrixRows[i];
   }
}

Comments

1

You have to create a new Matrix. As rows is an array of integer, you can simply use Arrays.copyOf(rows, rows.length) to copy it.

There are two common ways:

1/ Create a constructor which take a Matrix as input:

public Matrix(Matrix matrix){
  this.rows = Arrays.copyOf(matrix.rows, matrix.rows.length);
}

then Matrix k = new Matrix(x);

2/ implements Cloneable:

@Override
public Matrix clone() {
  Matrix m = new Matrix();
  m.rows = Arrays.copyOf(this.rows, this.rows.length);
  return m;
}

then Matrix k = x.clone();

Of course, you can also mix these two approaches :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.