请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Container 好像没有什么用啊

Iterator 的构造函数直接接收 list,这样的话就不需要 Container 类了。

class Iterator {
  constructor (list) {
    this.list = list
    this.index = 0
  }
  next () {
    if (this.hasNext()) {
      return this.list[this.index++]
    }
  }
  hasNext () {
    if (this.index >= this.list.length) {
      return false
    }
    return true
  }
}

let list = [1, 2, 3, 4, 5, 6]
let iterator = new Iterator(list)

while (iterator.hasNext()) {
  console.log(iterator.next())
}

正在回答

插入代码

1回答

代码中也没有 Container 类呀

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号