我有下面的代碼,這是造成了一個問題:配售的ArrayList內容到PriorityQueue中的Java問題
List<Node> tempList=new ArrayList<Node>(); //baseline
//creation of another temporary list of type Node for temporary storage
List<Node> tempList2=new ArrayList<Node>();
List<Node> temp = Adjacency_List.get(current.dest);
for(Node node: temp){
//testing
System.out.print(current.dest + " - " + node.dest);
System.out.println("\t\t("+Main.getEdge(current, node)+")");
for(int i=0; i<tempList.size(); i++){
//copying nodes from tempList into tempList2
tempList2.add(tempList.get(i));
System.out.println("TEMP LIST2 : "+tempList2.size());
}
tempList2.add(node);
System.out.println("TEMP LIST2 SIZE : "+tempList2.size());
cost=tempCost;
cost+=Main.getEdge(current, node);
n=new Node(tempList2, cost);
pq.add(n);
tempList2.clear();
}
這段代碼的基本目標是獲取當前節點的兒童(使用current.dest)和對於temp中的每個節點,它將tempList的內容複製到tempList2中(tempList也包含節點)。在將tempList2的內容添加到優先級隊列pq(pq.add(n))後,然後通過使用tempList2.clear()
進行清除後,會出現問題。優先級隊列pq內的tempList2的內容也被該行清除。有沒有一種方法可以清除tempList2數組列表的內容,而無需同時清除優先級隊列中的tempList2內容(以前通過使用行pq.add(n);添加到優先級隊列中)?