我有这门课,我不明白到底是怎么回事:
public class Table {
private int[][] array;
private int N;
// constructor
public Table(int[][] array) {
N = array.length;
this.array = Arrays.copyOf(array, N);
}
// this method returns another Table object
public Table tweak() {
int[][] tweak = Arrays.copyOf(array, N);
// here I change the array
return new Table(tweak);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我调用tweak方法时,用于调用方法的对象也会发生变化:
public class TestCls {
public static void main(String[] args) {
int[][] array = {{1, 2},
{3, 4}};
Table a = new Table(array);
System.out.println(a.toString());
/* this will print …Run Code Online (Sandbox Code Playgroud)