2013-04-20 140 views
-3

我是一個Java新手,試圖將一組活動(包括開始和結束時間)放入鏈接列表,然後優化這些活動將要進行的房間日程安排。我已鏈接列表創建,似乎我已經成功地將它傳遞到我的maxRoomUse方法中,我將執行時間表。我無法理解如何遍歷maxRoomUse方法中的列表。我不允許導入任何軟件包來幫助我。Java - 鏈接列表

+0

爲什麼你只是不添加一個getter到你的列表成員,並迭代它在需要它的地方?您也可以爲此作業實現迭代器 – giorashc 2013-04-20 12:05:45

+4

您已經在其add()及其toString()方法中迭代了列表。有什麼問題? – 2013-04-20 12:06:03

+0

這是作業還是作業?到目前爲止,您嘗試了什麼來遍歷列表? – Bobulous 2013-04-20 12:23:44

回答

0

您可以在List類中定義一個嵌套迭代器類。

ListIterator實現了Iterator,它有3個方法。

實現是這樣的。

public boolean next() { 
     return current == null; 
    } 

    public Activity next(){ 
     if (! this.hasNext()) 
      throw new IllegalStateException(); 
     //if (this.curModCount != modCount) 
     // throw new ConcurrentModificationException() 
     Activity data = this.current.activity; 
     this.current = this.current.next(); 
     return data; 
    } 

    public void remove(){ 
     throw new UnsupportedOperationException(); 
    }