How to copy iterator in Scala?

后端 未结 4 971
星月不相逢
星月不相逢 2021-01-01 17:03

About duplicate

This is NOT a duplicate of How to clone an iterator?

Please do not blindly close this question, all the answers given in so-called

4条回答
  •  萌比男神i
    2021-01-01 17:27

    As Rex said, it is impossible to make a copy of an Iterator without destroying it. That said, what is the problem with duplicate?

    var list = List(1,2,3,4,5)
    var it1 = list.iterator
    it1.next()
    
    val (it1a, it1b) = it1.duplicate
    it1 = it1a
    var it2 = it1b
    it2.next()
    
    println(it1.next())
    

提交回复
热议问题