Lecture 31: Iterators & useful List algorithms
Textbook chapters
7.4; 7.5.2
Useful List algorithms in Java
There are some useful methods in java.util.Collections class.
copy(L_dest, L_src)disjoint(C, D): boolean whetherC,Ddisjointmax(C): max element
and many more! Check out the documentation.
Converting between Lists and Arrays
- The
Listinterface has atoArray(A)method that returns an array of the same type as the array passed in (A), with the same elements as the list it was called on. java.util.Arrayshas anasList()method that takes in an array and returns a List of the same type, with the same elements.
Iterators
Iterators are a common design pattern in software. An iterator abstracts the process of scanning through a sequence of elements one at a time. In Java, iterators have these methods:
hasNext()next()
We already know an iterator well: Scanner!
Why use iterators?
If we define iterators to have a common interface, then any class that implements an iterator is guaranteed to have a common way that we can iterate over it. So we can reuse the same code to iterate over anything! (As long as it has an iterator.)
Java ArrayList iterator
- Can be implmented as a nested class.
- Also supports
remove