Java LinkedList listIterator() Method

The LinkedList.listIterator() method in Java is used to obtain a list iterator over the elements in a LinkedList. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. listIterator Method Syntax
  3. Examples
    • Obtaining a List Iterator
    • Using a List Iterator
    • List Iterator with a Starting Index
  4. Conclusion

Introduction

The LinkedList.listIterator() method is a member of the LinkedList class in Java. It allows you to obtain a list iterator to traverse the list in both forward and backward directions. This method is particularly useful for iterating over the list, modifying elements during iteration, and accessing elements from specific positions.

listIterator Method Syntax

The syntax for the listIterator method is as follows:

Obtaining a List Iterator from the Beginning

public ListIterator<E> listIterator()
  • Returns: A list iterator over the elements in this list (in proper sequence).

Obtaining a List Iterator from a Specific Position

public ListIterator<E> listIterator(int index)
  • index: The index of the first element to be returned from the list iterator (by a call to next).
  • Returns: A list iterator over the elements in this list (in proper sequence), starting at the specified position.

Examples

Obtaining a List Iterator

The listIterator method can be used to obtain a list iterator over the elements of a LinkedList.

Example

import java.util.LinkedList;
import java.util.ListIterator;

public class ListIteratorExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Obtaining a list iterator
        ListIterator<String> iterator = list.listIterator();

        // Printing elements using the list iterator
        System.out.println("LinkedList elements:");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

Output:

LinkedList elements:
Apple
Banana
Orange

Using a List Iterator

A list iterator can be used to traverse the list in both forward and backward directions, modify elements during iteration, and access elements from specific positions.

Example

import java.util.LinkedList;
import java.util.ListIterator;

public class ListIteratorUsageExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Obtaining a list iterator
        ListIterator<String> iterator = list.listIterator();

        // Traversing the list forward
        System.out.println("Forward traversal:");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // Traversing the list backward
        System.out.println("Backward traversal:");
        while (iterator.hasPrevious()) {
            System.out.println(iterator.previous());
        }

        // Modifying elements during iteration
        while (iterator.hasNext()) {
            String element = iterator.next();
            if (element.equals("Banana")) {
                iterator.set("Mango");
            }
        }

        // Printing the modified LinkedList
        System.out.println("Modified LinkedList: " + list);
    }
}

Output:

Forward traversal:
Apple
Banana
Orange
Backward traversal:
Orange
Banana
Apple
Modified LinkedList: [Apple, Mango, Orange]

List Iterator with a Starting Index

You can obtain a list iterator starting at a specific position in the LinkedList.

Example

import java.util.LinkedList;
import java.util.ListIterator;

public class ListIteratorWithIndexExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Obtaining a list iterator starting at index 1
        ListIterator<String> iterator = list.listIterator(1);

        // Printing elements from the specified position
        System.out.println("LinkedList elements from index 1:");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

Output:

LinkedList elements from index 1:
Banana
Orange

Conclusion

The LinkedList.listIterator() method in Java provides a way to obtain a list iterator to traverse the list in both forward and backward directions. By understanding how to use this method, you can efficiently iterate over elements, modify them during iteration, and access elements from specific positions within your Java applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top