Java 怪异的阿雷利主义行为
我创建了3个芯片组对象,并将它们放在ArrayList中。arraylist不包含正确的值。调试消息非常清楚发生了什么,但我对这种行为没有任何解释。有人能告诉我我的错误吗? 这是我的密码:Java 怪异的阿雷利主义行为,java,arraylist,behavior,Java,Arraylist,Behavior,我创建了3个芯片组对象,并将它们放在ArrayList中。arraylist不包含正确的值。调试消息非常清楚发生了什么,但我对这种行为没有任何解释。有人能告诉我我的错误吗? 这是我的密码: import java.util.ArrayList; import java.util.Arrays; public class WierdArrayBehaviour { public static void main(String[] args) { ArrayList<
import java.util.ArrayList;
import java.util.Arrays;
public class WierdArrayBehaviour {
public static void main(String[] args) {
ArrayList<ChipSet> chipSetCombos = createComboExample();
System.out.printf("\n\n---- Show created combolist ----");
System.out.printf("\nCombo 1: " + Arrays.toString(chipSetCombos.get(1).getChips()));
System.out.printf("\nCombo 2: " + Arrays.toString(chipSetCombos.get(1).getChips()));
System.out.printf("\nCombo 3: " + Arrays.toString(chipSetCombos.get(2).getChips()));
}
private static ArrayList<ChipSet> createComboExample() {
ArrayList<ChipSet> combos = new ArrayList<ChipSet>();
System.out.printf("---- Creating possible combos ----");
ChipSet combo1 = new ChipSet(new int[]{1, 1, 1, 1, 2});
System.out.printf("\nCombo 1: " + Arrays.toString(combo1.getChips()));
ChipSet combo2 = new ChipSet(new int[]{1, 1, 1, 1, 3});
System.out.printf("\nCombo 2: " + Arrays.toString(combo2.getChips()));
ChipSet combo3 = new ChipSet(new int[]{1, 1, 1, 1, 4});
System.out.printf("\nCombo 3: " + Arrays.toString(combo3.getChips()));
combos.add(combo1);
combos.add(combo2);
combos.add(combo3);
return combos;
}
}
class ChipSet {
public static final int WHITE_VALUE = 1;
public static final int RED_VALUE = 2;
public static final int GREEN_VALUE = 5;
public static final int BLUE_VALUE = 10;
public static final int BLACK_VALUE = 20;
public static final int[] VALUES = new int[]{WHITE_VALUE, RED_VALUE, GREEN_VALUE, BLUE_VALUE, BLACK_VALUE};
protected static int[] chips;
public ChipSet(int[] chips) {
if (chips == null || chips.length != 5) {
throw new IllegalArgumentException("ChipSets should contain exactly 5 integers!");
}
// store a copy of passed array
this.chips = new int[5];
for (int i = 0; i < this.chips.length; i++) {
this.chips[i] = chips[i];
}
}
public int getSum() {
return chips[0] * WHITE_VALUE
+ chips[1] * RED_VALUE
+ chips[2] * GREEN_VALUE
+ chips[3] * BLUE_VALUE
+ chips[4] * BLACK_VALUE;
}
public int[] getChips() {
return this.chips;
}
}
您每次都在打印combo1。换成
System.out.printf("\nCombo 2: " + Arrays.toString(combo2.getChips())); // <-- 2
ChipSet combo3 = new ChipSet(new int[]{1, 1, 1, 1, 4});
System.out.printf("\nCombo 3: " + Arrays.toString(combo3.getChips())); // <-- 3
编辑
改变
protected static int[] chips;
到
实际上,所有实例只有一个芯片数组。发生的是,属性芯片是静态的,这意味着每个类只存在一个 每次创建新的芯片组实例时,都会覆盖以前创建的芯片 你能做什么?不要将其声明为静态:
在createComboExample中,您每次都使用combo1,并且您的索引在main中不一致。请注意,Stack Overflow无意成为众包的“我的打字错误”服务……你看到了什么?你希望看到什么?你可以在索引1,1,2上打印主要调用的内容-你可能是说0,1,2@OliverCharlesworth很抱歉再试一次,我发布了一个错误的示例。好的,请构造一个完整的测试用例,我们可以直接将粘贴复制到在线编译器中,例如,并告诉我们症状是什么。Lol对没有添加2个导入和一个类的否决票表示欢迎。@ManyQuestions已编辑。下次,为了更好更快地提供帮助,请发布正确的代码!我接受了另一个,因为它让我更好地理解,但还是非常感谢你。
protected static int[] chips;
protected int[] chips;
protected int[] chips;