Sorry for my elementary Java question. There is the following class
public class Matrix {
public final double[][] items;
private final int rows_count, columns_count;
public Matrix( final int rows_count_, final int columns_count_) {
rows_count = rows_count_; columns_count = columns_count_;
items = new double[rows_count][columns_count];
}
public Matrix(final double[][] data) {
rows_count = data.length;
columns_count = data[0].length;
items = new double[rows_count][columns_count];
for (int i = 0; i < rows_count; i++)
for (int j = 0; j < columns_count; j++)
}
public Matrix copy () {
Matrix AC = new Matrix(rows_count, columns_count);
for (int i = 0; i < rows_count; i++)
for (int j = 0; j < columns_count; j++)
AC.items[i][j] = items[i][j];
return AC;
}
public Matrix clone () { return this.copy }
public void test (Matrix B) {
B = this.clone();
B.items[0][0] = 1;
}
Inside the method test the following assignment is done
B = A
Calling
double[][] d = { { 1, 2, 3 }, { 4, 5, 6 }, { 1, 0, 1} };
Matrix A = new Matrix(d);
Matrix B= new Matrix(3,3);
A.test(B);
B.print();
the results are surprising. Despite
B = this.clone()
the resulting B matrix is of zeros.
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
It looks as if B is passed by the value :-) Rewriting test() so as
public void test (Matrix B) {
B.items[0][0] = 1;
}
the matrix B is modified correctly
1.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
Where is the problem, probably incorrectly written copy/clone method? How to fix the problem and performs an assignment B = A? Thanks for your help.