Iterators

This is the parent or base interface in the iterator hierarchy. This iterator can be accessed by any list using list.iterator(). This is why iterators can work with lists, collections, and mutable lists:

The iterator contains two functions that help to access elements:

  • hasNext(): This returns true if the iterator finds an item to iterate on. Otherwise, it returns false.
  • next(): This returns an element of the list and moves the cursor forward:
fun iteratorFunction() {
val list = listOf(1,2,3,4,5)
var listIterator = list.iterator()

while (listIterator.hasNext()) {
println(listIterator.next())
}
}

Use the hasNext function to verify whether an item is in the list. You can get an element using the next() function. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.164