Java Object-Array copy with 1 less
Java Object-Array copy with 1 less
public void arrayUpdate(int counter,int counter2){
int counterShipMemory=0;
this.playerMemory2= new Player[this.playerMemory.length];
for(int i = 0; i< this.playerMemory.length;i++){
this.playerMemory2[i] = this.playerMemory[i];
}
this.playerMemory2[counter].shipMemory= new Ship[this.playerMemory2[counter].shipMemory.length-1];
for(int counter3=0;counter3 <this.playerMemory[counter].shipMemory.length;counter3++){
if(counter3!=counter2){
this.playerMemory2[counter].shipMemory[counterShipMemory]=this.playerMemory[counter].shipMemory[counter3];
counterShipMemory++;
}
}
this.playerMemory[counter].shipMemory = new Ship[playerMemory2[counter].shipMemory.length];
for(int counter3=0;counter3 <playerMemory2[counter].shipMemory.length;counter3++){
this.playerMemory[counter].shipMemory[counter3]=playerMemory2[counter].shipMemory[counter3];
}
说明:
counter
是一个玩家的数组 ID。
counter2
是应该移除的飞船的数组 ID。
问题在第 7 行 (this.playerMemory2[counter].shipMemory= new Ship
)。
如果我开始这一行,playerMemory2
处的 shipMemory
和 playerMemory
会发生变化。但为什么?我该如何解决这个问题?
它们都改变了,因为它们是同一个对象:
for(int i = 0; i < this.playerMemory.length; i++){
this.playerMemory2[i] = this.playerMemory[i];
}
在这个循环中,两个数组引用相同的对象(playerMemory[i] == playerMemory2[i]
,相同内存地址的对象)。因此,如果你改变了playerMemory2[i]
的shipMemory
,你也改变了playerMemory[i]
的shipMemory
。
为防止这种情况,您可以克隆每个对象。如果您实现了 clone()
方法,这将是一个简单的修复方法:
for(int i = 0; i < this.playerMemory.length; i++){
this.playerMemory2[i] = this.playerMemory[i].clone();
}
现在,只有 playerMemory2[i]
的 shipMemory
会改变。
public void arrayUpdate(int counter,int counter2){
int counterShipMemory=0;
this.playerMemory2= new Player[this.playerMemory.length];
for(int i = 0; i< this.playerMemory.length;i++){
this.playerMemory2[i] = this.playerMemory[i];
}
this.playerMemory2[counter].shipMemory= new Ship[this.playerMemory2[counter].shipMemory.length-1];
for(int counter3=0;counter3 <this.playerMemory[counter].shipMemory.length;counter3++){
if(counter3!=counter2){
this.playerMemory2[counter].shipMemory[counterShipMemory]=this.playerMemory[counter].shipMemory[counter3];
counterShipMemory++;
}
}
this.playerMemory[counter].shipMemory = new Ship[playerMemory2[counter].shipMemory.length];
for(int counter3=0;counter3 <playerMemory2[counter].shipMemory.length;counter3++){
this.playerMemory[counter].shipMemory[counter3]=playerMemory2[counter].shipMemory[counter3];
}
说明:
counter
是一个玩家的数组 ID。
counter2
是应该移除的飞船的数组 ID。
问题在第 7 行 (this.playerMemory2[counter].shipMemory= new Ship
)。
如果我开始这一行,playerMemory2
处的 shipMemory
和 playerMemory
会发生变化。但为什么?我该如何解决这个问题?
它们都改变了,因为它们是同一个对象:
for(int i = 0; i < this.playerMemory.length; i++){
this.playerMemory2[i] = this.playerMemory[i];
}
在这个循环中,两个数组引用相同的对象(playerMemory[i] == playerMemory2[i]
,相同内存地址的对象)。因此,如果你改变了playerMemory2[i]
的shipMemory
,你也改变了playerMemory[i]
的shipMemory
。
为防止这种情况,您可以克隆每个对象。如果您实现了 clone()
方法,这将是一个简单的修复方法:
for(int i = 0; i < this.playerMemory.length; i++){
this.playerMemory2[i] = this.playerMemory[i].clone();
}
现在,只有 playerMemory2[i]
的 shipMemory
会改变。