Get startedGet started for free

Modifying lists with ListIterator

1. Modifying lists with ListIterator

In the last video, we explored `Iterator` for iterating through collections. Now, we extend this concept with `ListIterator`, which provides more control over list traversal.

2. Iterator vs ListIterator

Unlike a regular `Iterator`, `ListIterator` allows moving forward and backward within a list, modifying existing elements and adding new elements dynamically. However, `ListIterator` only works for `List` collections while `Iterator` works for all data collections.

3. Sample ArrayList

To start, we will create an `ArrayList` named `names`, which contains `Alice`, `Bob` and `Charlie`, to showcase `ListIterator` functionality.

4. Traversing a list with ListIterator

To iterate through the list, we start by importing the `ListIterator` class Then, we call `.listIterator()` on the `names` list to create a `ListIterator` object. After that, we use `.hasNext()` and `.next()` to move forward through the list, printing each element. As a result, the code outputs all the names line by line

5. Moving backward with ListIterator

One of `ListIterator`’s key advantages is moving backward through a list. If we need to traverse backward, we must initialize `ListIterator` at the end of the list by calling `.listIterator(names.size())`. Instead of `.hasNext()`, we use `.hasPrevious()` to check if an earlier element exists. Calling `.previous()` moves the iterator backward and retrieves the previous element so we can print it.

6. Modifying elements during iteration

With `ListIterator`, we can modify elements while iterating using the `.set()` method. First, we call `.listIterator()` on the `names` list to create a `ListIterator` object. Then we loop through `names`, checking if an element equals `Bob`. When found, we replace `Bob` with `Bobby` using `.set()`. After modification, the `names` list now contains 3 elements: `Alice`, `Bobby` and `Charlie`.

7. Adding elements during iteration

With `ListIterator`, we can insert elements dynamically using the `.add()` method. First, we call `.listIterator()` on the `names` list to create a `ListIterator` object. Then, we traverse `names`, checking for `Charlie`. When `Charlie` is found, `David` is inserted right after position of `Charlie`. After insertion, the `names` list now contains 4 elements: `Alice`, `Bobby`, `Charlie` and `David`.

8. Summary

In this video, we explored `ListIterator`, an enhanced version of `Iterator` that allows both forward and backward traversal while modifying lists safely. We covered key operations like `.next()` and `.previous()` to navigate, `.set()` to modify elements, `.add()` to insert, and `.remove()` for safe deletion. Since `ListIterator` only works on Lists, it provides more flexibility than a standard Iterator but is not compatible with `Set` or `Map`.

9. Let's practice!

Now it's time for you to practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.