我想知道是否有类test1的两个数组testSet和testSet2 在方法问题()中,testSet初始化为1,testSet2初始化为0 在使用testSet对testSet2进行均衡后,testSet2长度与testSet相同,这很好 但 在方法problem1()中,testSet初始化为1,testSet2初始化为0 并且在方法equize()中均衡后,testSet2与testSet的testSet2长度变为0。 为什么呢?
public class test1
{
// C
public test1(int _data)
{
data = _data;
}
// Field
public int data;
}
// reference equalize method
public void equalize(test1[] a,test1[] b)
{
a = b;
}
public test1[] testSet,testSet2; // arrays
// called from main method access
public void Problem()
{
// initializing array
testSet = new test1[1];
testSet2 = new test1[0];
testSet [0] = new test1 (11);
testSet2 = testSet;
// testSet2.length = 1 which is fine
}
// called from main method access
public void Problem1()
{
testSet = new test1[1];
testSet2 = new test1[0];
testSet [0] = new test1 (11);
equalize (testSet2,testSet);
// want to know why this is happening ?
// testSet2.length = 0
// in problem() length = 1 but here is 0 why?
}
答案 0 :(得分:0)
testSet2 = 0的原因是因为方法equalize()只在里面使用它们。 methods()仅代表他们使用参数。
如果要更改testSet2的长度。
启动它 public TestClass(int _data)
{
data = _data;
testSet2 = new TestClass[0];
}
and when you want to change it's length try this
public void Problem1()
{
testSet = new TestClass[1];
testSet [0] = new TestClass (11);
equalize (testSet);
System.out.println(testSet2.length);
System.out.println(testSet.length);
}
public void equalize(TestClass[] b)
{
testSet2 = b;
}