ArrayList add方法正在覆盖列表中的对象

时间:2022-06-13 21:19:55

So, when I try adding a Trick to an ArrayList the Trick that is added overwrites what is already in the ArrayList so that all the elements in the ArrayList consists of the trick I have just added.

因此,当我尝试将一个Trick添加到ArrayList时,添加的Trick会覆盖ArrayList中已有的内容,以便ArrayList中的所有元素都包含我刚刚添加的技巧。

These methods are in my main class:

这些方法在我的主要课程中:

public Trick playTrick(Player firstPlayer){
     Trick t=new Trick(firstPlayer.getID());
        return t;

}


public void playGame(){

    for(int i=0;i<NOS_TRICKS;i++){

        Trick t=playTrick(players[firstPlayer]);
        players[firstPlayer].update(t);
        //System.out.println(t.completedTricks);
    }

}

The error is occurring when players[firstPlayer].update(t) is called, update(t) adds Trick t to my ArrayList:

调用player [firstPlayer] .update(t)时发生错误,update(t)将Trick t添加到我的ArrayList:

public class BPlayer implents Player{
   public void update(Trick t){
       this.strategy.updateData(t);
   }
}

This then triggers updateData:

然后触发updateData:

public class AdvStrategy implements Strategy{
    public static ArrayList<Trick> completedTricks = new ArrayList<>();

    @Override
    public void updateData(Trick c) {
        completedTricks.add(c);
    }
}

Example of my output when printing the ArrayList:

打印ArrayList时输出的示例:

Trick ={3=KING DIAMONDS, 0=TWO DIAMONDS, 1=THREE DIAMONDS, 2=FIVE DIAMONDS}
Lead =3 Winner =3 DIAMONDS
ARRAYLIST[{3=KING DIAMONDS, 0=TWO DIAMONDS, 1=THREE DIAMONDS, 2=FIVE DIAMONDS}]
Trick ={3=QUEEN DIAMONDS, 0=FOUR DIAMONDS, 1=SIX DIAMONDS, 2=TEN DIAMONDS}
Lead =3 Winner =3 DIAMONDS
ARRAYLIST[{3=QUEEN DIAMONDS, 0=FOUR DIAMONDS, 1=SIX DIAMONDS, 2=TEN DIAMONDS}, {3=QUEEN DIAMONDS, 0=FOUR DIAMONDS, 1=SIX DIAMONDS, 2=TEN DIAMONDS}]

Last line of output should be this when working...

工作的最后一行应该是这个......

ARRAYLIST[{3=KING DIAMONDS, 0=TWO DIAMONDS, 1=THREE DIAMONDS, 2=FIVE DIAMONDS}, {3=QUEEN DIAMONDS, 0=FOUR DIAMONDS, 1=SIX DIAMONDS, 2=TEN DIAMONDS}]

1 个解决方案

#1


1  

When you write Trick t this is a reference to a Trick and when you add this reference to an ArrayList, the reference is copied not the object it references. This means if you add the same reference to a List more than once and you alter the object it references, it appears that all object have changed before there is actually only one.

当您编写Trick时,这是对Trick的引用,当您将此引用添加到ArrayList时,将复制引用而不是它引用的对象。这意味着如果您多次向List添加相同的引用并更改它引用的对象,则看起来所有对象在实际只有一个之前已经更改。

Somewhere in your code, you are adding the same reference more than once. I suggest you step through your code in your debugger to see exactly where this happens.

在代码中的某个位置,您不止一次添加相同的引用。我建议您在调试器中逐步执行代码,以确切了解这种情况发生的位置。

Alternatively you could check whether the Trick you are about to add is already in the list and throw an error if it is.

或者,您可以检查您要添加的Trick是否已经在列表中,如果是,则抛出错误。

#1


1  

When you write Trick t this is a reference to a Trick and when you add this reference to an ArrayList, the reference is copied not the object it references. This means if you add the same reference to a List more than once and you alter the object it references, it appears that all object have changed before there is actually only one.

当您编写Trick时,这是对Trick的引用,当您将此引用添加到ArrayList时,将复制引用而不是它引用的对象。这意味着如果您多次向List添加相同的引用并更改它引用的对象,则看起来所有对象在实际只有一个之前已经更改。

Somewhere in your code, you are adding the same reference more than once. I suggest you step through your code in your debugger to see exactly where this happens.

在代码中的某个位置,您不止一次添加相同的引用。我建议您在调试器中逐步执行代码,以确切了解这种情况发生的位置。

Alternatively you could check whether the Trick you are about to add is already in the list and throw an error if it is.

或者,您可以检查您要添加的Trick是否已经在列表中,如果是,则抛出错误。