对象克隆和了解引用中的底层对象 (JAVA)
我正在尝试根据“生命游戏”模拟概念编写源代码。我所做的是创建一个类来表示游戏环境状态,但将后继函数的逻辑分离到不同的类中。游戏的状态保存在一个简单的多维布尔数组中。
后继函数应该做的是将当前状态保存在新数组中作为原始数组的克隆,并在整个过程中使用它作为基础。然后,它以游戏的当前状态为参考,并使用游戏逻辑对其进行修改。我的问题是我将原始变量克隆到一个新变量中,但即使这个克隆也会被后继函数修改。
我不太确定发生了什么,所以我无法正确调试。我更习惯 C# 对原始类型克隆的处理。
下面是我的后继函数包装类的代码:
public class Successor {
boolean[][] current_state;
public void next(WorldPopulation worldState){
int dim_x = worldState.get_width();
int dim_y = worldState.get_height();
boolean[][] nextstate = worldState.get_state();
current_state = worldState.get_state().clone();
for(int i=0; i<dim_y; i++){
for(int j=0; j<dim_x; j++){
int neighbors = 0;
neighbors = countThyNeighbors(i, j);
if((neighbors>3 || neighbors<3) && current_state[i][j]){
nextstate[i][j] = false;
} else if(!current_state[i][j] && neighbors>=3) { nextstate[i][j] = true; }
}
}
}
private int countThyNeighbors(int y, int x) {
int[] grid_x = { x-1, x, x+1, x-1, x+1, x-1, x, x+1};
int[] grid_y = { y-1, y-1, y-1, y, y, y+1, y+1, y+1};
int neighbors = 0;
for(int i=0; i<8; i++){
try{
if(current_state[grid_y[i]][grid_x[i]])
neighbors++;
} catch(Exception e) { continue;}
}
return neighbors;
}
}
WorldPopulation 类中的 get() 和 set() 函数仅仅是赋值和返回操作。
我从 C# 背景了解一些克隆(深克隆和浅克隆)的细微差别,但我对 Java 范例完全陌生。
非常感谢任何改进我的代码的建议,但我想知道发生了什么以及我做错了什么。非常感谢! :D
I am trying to write source based on the "Game Of Life" simulation concept. What I did was create a class to represent the game environment state but separated the logic of the successor function in a different class. The state of the game is saved in a simple multidimensional array of booleans.
What the successor function is supposed to do is save the current state in a new array as a clone of the original and use this as basis throughout the process. It then takes the current state of the game as a reference and modifies this using the game logic. My problem is that I cloned the original into a new variable, but even this clone gets modified by the successor function.
I'm not so sure what's happening so I can't debug properly. I'm more accustomed to C#'s handling of primitive types clones.
Here is my code for the successor function wrapper class:
public class Successor {
boolean[][] current_state;
public void next(WorldPopulation worldState){
int dim_x = worldState.get_width();
int dim_y = worldState.get_height();
boolean[][] nextstate = worldState.get_state();
current_state = worldState.get_state().clone();
for(int i=0; i<dim_y; i++){
for(int j=0; j<dim_x; j++){
int neighbors = 0;
neighbors = countThyNeighbors(i, j);
if((neighbors>3 || neighbors<3) && current_state[i][j]){
nextstate[i][j] = false;
} else if(!current_state[i][j] && neighbors>=3) { nextstate[i][j] = true; }
}
}
}
private int countThyNeighbors(int y, int x) {
int[] grid_x = { x-1, x, x+1, x-1, x+1, x-1, x, x+1};
int[] grid_y = { y-1, y-1, y-1, y, y, y+1, y+1, y+1};
int neighbors = 0;
for(int i=0; i<8; i++){
try{
if(current_state[grid_y[i]][grid_x[i]])
neighbors++;
} catch(Exception e) { continue;}
}
return neighbors;
}
}
The get() and set() functions in the WorldPopulation class are merely assignment and return operations.
I know a bit about nuances of cloning (deep and shallow clones) from a C# background, but I am totally new in Java paradigms.
Any suggestions to improve my code is much appreciated, but I want to know what's happening and what I'm doing wrong. Thank you very much! :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多维数组表示为数组的数组。
Java 中的克隆方法是浅层的。这意味着多维数组没有被深度克隆。
您可以实现自己的深度复制方法或使用此处所述的序列化
Multi-dimensional arrays are represented as arrays of arrays.
A clone method in Java is shallow. that means multidimentional array is not cloned deeply.
You can implement your own deep copy method or use serialization as descibed here