奇怪的ArrayList行为

时间:2014-12-26 15:56:39

标签: java arraylist behavior

我创建3个ChipSet对象并将它们放在ArrayList中。 arraylist不包含正确的值。 debug messages对发生的事情非常清楚,但我根本没有解释这种行为。有人能告诉我我的错误吗? 这是我的代码:

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

2 个答案:

答案 0 :(得分:4)

您的属性chipsstatic,这意味着每个类都存在一次。

每次创建新的Chipset实例时,都会覆盖之前创建的chips

你能做什么?不要将其声明为static

protected int[] chips;

答案 1 :(得分:3)

您每次都在打印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

System.out.printf("\nCombo 2: " + Arrays.toString(combo1.getChips()));
ChipSet combo3 = new ChipSet(new int[]{1, 1, 1, 1, 4});
System.out.printf("\nCombo 3: " + Arrays.toString(combo1.getChips()));

修改

更改

protected static int[] chips;

protected int[] chips;

因为对于所有实例,只有一个Chips数组。