我有一個小的Java應用程序,將執行一些數學函數。無論如何,我有這個代碼創建點並將它們添加到另一個結構中,以便稍後使用這些值。這是代碼鏈接列表非常奇怪的刪除
/*for the sake of the example, this part is fine. these two variables are filled
earlier but this is how they are initialized */
LinkedList<Point> col = new LinkedList<Point>();
LinkedList<Point> row = new LinkedList<Point>();
/* Issue area below */
LinkedList<Point> added = new LinkedList<Point>();
while(!col.isEmpty()){
LinkedList<Point> tempRow = row;
while(!tempRow.isEmpty()){
added.add(new Point(col.getFirst().x,tempRow.getFirst().y));
tempRow.remove();
}
col.remove();
}
當運行此代碼行tempRow.remove()
以某種方式從實際行取出爲好。這對我來說沒有意義,因爲我創建了一個局部變量temp並調用了THAT實例變量。有人可以解釋爲什麼會這樣嗎?
謝謝你的提示!現在你能解釋爲什麼刪除臨時變量會從兩者中刪除嗎? – user3704079 2014-11-08 03:21:32