当我在将 Array<> 的项目分配给临时变量后更改其值时,主要变量值也会更改。
Array<Cards> cards = new Array<Cards>();
//add items to cards
Iterator<Cards> iterator = cards.iterator();
while(iterator.hasNext()){
Cards c = iterator.next();
Cards Temp = c;
//when I change values of temp...the value of c also changes
//overall changing the value of cards
}
有什么方法可以改变 Temp 而不是 c 或 cards 的值?
我目前正在为 Android 使用 libgdx 制作游戏。
最佳答案
您需要复制c
引用的对象。您当前正在做的只是创建对同一对象的另一个引用。根据 Card
实现和您的需要,您可以执行克隆
或显式创建新的Card
。
关于java - 在 Java Array<> libgdx 方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31661635/