2015-10-30 53 views
0

編輯:我相信問題是在添加節點。當我開始讀取節點時,它們返回null。我相信這是因爲head不會被設置爲impl.getHeadNode()== null從不發生。節點getter不返回setter值

構造:

public CashDonationLinkedListImpl() { 
iterNode = headNode = new CashDonationNode(null); 
} 

添加方法:

public void add(Object obj) { 
    CashDonation cashDonation = (CashDonation)obj; 
    CashDonationLinkedListImpl impl = new CashDonationLinkedListImpl(); 

    CashDonationNode node = new CashDonationNode(); 
     node.setCashDonation(cashDonation); 
     //node.setNext(null); 
    if(impl.getHeadNode() == null){ 

     System.out.println("Head node"); 
     impl.setHeadNode(node); 

     } 
     else{ 
     //node.setCashDonation(cashDonation); 
     //node.setNext(null); 
     for (CashDonationNode cashNode = headNode;cashNode.getNext() != null;cashNode = cashNode.getNext()) { 
      // currNode.setNext(null); 
     }   

      //cashNode.setCashDonation(cashDonation); 

     cashNode.setNext(node); 

     } 

原文: getter返回兩個零,而二傳發生在100,100和10000。我敢肯定,我忘記做某事,但不能把我的手指放在上面。爲什麼不是getter返回setter的值?

public class CashDonationNode { 
    CashDonation cashDonation; 
    CashDonationNode next; 

    public CashDonationNode() { 
     cashDonation = new CashDonation(); 
     next = null; 
    } 

    public CashDonationNode(CashDonation cashDonation, CashDonationNode next) { 
     this.cashDonation = cashDonation; 
     this.next = next; 
    } 

    public CashDonationNode getNext(){ 
     return next; 
    } 
    public void setNext(CashDonationNode nextValue){ 
     this.next = nextValue; 
    } 
    public void setCashDonation(CashDonation cashDonation){ 
     this.cashDonation = cashDonation; 
     System.out.print("node storage"); 
     System.out.println(this.cashDonation.getDonationAmount()); 
    } 

    public CashDonation getCashDonation(){ 
     System.out.println("node retrieval " + cashDonation.getDonationAmount()); 
       return this.cashDonation; 
    } 
} 

下面鏈接列表的代碼片段。返回相同的零:

CashDonationNode iterNode = new CashDonationNode(); //declared in class 
public void iterator() { 
    iterNode = headNode; 
} 

@Override 
public CashDonation next() { 
CashDonation cashDonation = iterNode.getCashDonation(); 
System.out.println("Impl: " + cashDonation.getDonationAmount()); 
    iterNode = iterNode.getNext(); 
    return cashDonation; 
} 
+0

沒有堅持在這裏。請包含練習此類的代碼和「CashDonation」類(並刪除任何不相關的代碼)。 A [mcve],所以我們可以看到自己的問題。 –

+0

不確定什麼都適合包括在內。節點是鏈接列表的一部分,由管理員調用。經理從主類的一部分被調用。 CashDonation類在處理數組時能夠正常工作,並且自那時以來沒有改變過。開始時將包含那些處理next()的部分。 –

+0

@Nicholas Yount你可以提供'CashDonation'類嗎?這樣我們可以弄清楚爲什麼getter返回意外的值。 – Truthira

回答

0

修正的add():

public void add(Object obj) { 
    CashDonation cashDonation = (CashDonation)obj; 
    CashDonationNode node = new CashDonationNode(cashDonation); 
    this.setIterNode(node); 

    if(this.getHeadNode() == null){ 
     this.setHeadNode(node); 
     node.setNext(null); 
     } 
     else{ 
     CashDonationNode cashNode; 
     for (cashNode = headNode;cashNode.getNext() != null;cashNode = cashNode.getNext()) { 
     }   
     cashNode.setNext(node); 
     } 
    numElements++; 

}