我有一个包含整数列表的类。我想打印不同的整数组。但即使引用变量不同,我的代码也会打印完全相同的数字。这是我的代码:
import java.util.ArrayList;
public class OrderedIntListUtility {
public static void printContains(OrderedIntList list) {
for(int i = 0; i < list.orderedIntegerList.length; i++) {
System.out.print(list.orderedIntegerList[i]);
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
public class OrderedIntList {
static int[] orderedIntegerList;
public OrderedIntList(int ... elements) {
orderedIntegerList = elements;
}
public OrderedIntList() {
orderedIntegerList = null;
}
}
public class TestOrderedIntList {
public static void main(String[] args) {
OrderedIntListUtility operate = new OrderedIntListUtility();
OrderedIntList listOfA = new OrderedIntList(2,3,1,55,77);
OrderedIntList listOfB = new OrderedIntList(2,3,5,77);
operate.printContains(listOfA);
System.out.println();
operate.printContains(listOfB);
}
}
问题是operations.printContains(listOfA)打印listOfB orderedIntList。我很迷惑。它们的变量名称不同吗?请帮忙。谢谢!
答案 0 :(得分:1)
在课程OrderedIntList
中,您static int[] orderedIntegerList;
将其更改为int[] orderedIntegerList;
,这样就可以了。静态意味着类级别,而不是实例级别