gpt4 book ai didi

Java:打印出最后一个引用变量

转载 作者:行者123 更新时间:2023-12-01 12:32:01 35 4
gpt4 key购买 nike

我有一个包含整数列表的类。我想打印不同组的整数。但即使引用变量不同,我的代码也会打印完全相同的数字。这是我的代码:

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);

}
}

问题是 opera.printContains(listOfA) 打印 listOfB OrderedIntList。我很困惑。它们的变量名不同,对吗?请帮忙。谢谢!

最佳答案

OrderedIntList类中,您有static int[]orderedIntegerList;将其更改为int[]orderedIntegerList;并且它将起作用。静态意味着在级别可用,而不是在实例级别

关于Java:打印出最后一个引用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25861542/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com