2013-04-20 153 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(); 
    }