我怀疑返回的值是我添加到我的数组列表中的最后一个。ArrayList为每个索引返回相同的元素
我有一个类Game
在下面的函数被执行...
public List<M> compute() throws CloneNotSupportedException{
G chil = (G) this.clone();
List<Move> Pm = new ArrayList<M>();
Point p;
for(/*condition*/){
//p is defined up here.basically loops through all possible values of p.
M m = pud.genM(chil, p);
Pm.add(m);
}
return Pmoves;
}
奇怪的是我正在做的事情正在返回m
值的打印和他们预期(不同),但是当我遍历已创建的List时,每个元素都等于最后添加的元素。看起来最后被添加的元素覆盖了之前所有的孩子...
变量pud
的类型为User
。被调用的函数是...
public M genM(GameState g, boardPoint p){
M m; // a move
for(/*condition*/){
m = new M(/*parameters*/);
return m;
}
return null;
}
我已经简化了这一堂课下来公平一点,有一些的if else条件和额外的循环,但我把他们从这个例子的,因为他们是那种凌乱和最有可能不相关。
对象M
被定义为...
public M(String type, Point fPos, Point tPos, String input, G g){
this.type = type;
this.toPos = toPos;
this.fromPos = fromPos;
this.uInput = uInput;
result = new G(g);
//value of G gets edited here.
}
你可以写一个自包含的程序来重现问题吗?编写一个简单的程序,显示问题并保持简化,直到不能显示问题。 – 2011-12-15 15:53:12
你在示例代码中调用`genM`,但你已经显示`genMenMoves` - 它们的意思是相同的方法吗? (我真的希望你的名字在你的实际代码中更清楚) – 2011-12-15 15:53:27