-3
public CarList(CarList cl)
{
if(cl == null) throw new NullPointerException();
if (cl.head == null)
head = null;
else
{
// Notice that you cannot issue head = cl.head; otherwise both heads will point at the passed list;
head = null;
// Now create and copy all the nodes in the list
CarNode temp, temp2, temp3;
temp = cl.head;
temp2 = null;
temp3 = null;
while(temp != null)
{
if (temp2 == null) // The case for the first node
{
temp2 = new CarNode(temp.getCar(), null);
head = temp2;
}
else
{
temp3 = new CarNode(temp.getCar(), null);
temp2.setNext(temp3);
temp2 = temp3;
}
temp = temp.getNext();
}
// Avoid privacy leak; set all temporary pointers to null
temp = temp2 = temp3 = null;
}
}
我不太明白環路做什麼......我不能解析的代碼。隱私權是否是由於臨時變量包含地址這一事實造成的?爲什麼我們在循環中指定一個值,如果它沒有被使用?
呃,'head'使用。看到這一行:'head = temp2;' – jrd1
它從來沒有用在_ code_;然而,'head'是CarList類的一個成員字段,並且(採用有教養的猜測)在課程的其他地方使用。 –