我想知道类test1的两个数组testSet和testSet2
在方法problem()中,testSet初始化为1,而testSet2初始化为0
在用testSet均衡testSet2之后,testSet2的长度与testSet相同,这很好
但
在方法problem1()中,将testSet初始化为1,将testSet2初始化为0
并且在方法equalize()中均衡后,使用testSet的testSet2的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?
}
最佳答案
之所以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;
}