LinkedList listIterator() method in Java

Priyanka Patwal
Last Updated: May 13, 2022

Java Iterator

Java iterator is an object of the Iterator class in java.utils package. As the name suggests, it is used to iterate over the Collection items such as ArrayList, LinkedList etc.

The iterator() method is used to get an iterator of any collection item.

For example:

import java.util.ArrayList;
import java.util.Iterator;

public class Main {
  public static void main(String[] args) {
  
    // Make a ArrayList
    ArrayList<String> str= new ArrayList<String>();
    cars.add("Coding");
    cars.add("Ninjas");
    cars.add("Studio");
    cars.add("Blog");

    // Getting the iterator
    Iterator<String> itr = str.iterator();

    // Printing the first item
    System.out.println(it.next());
  }
}

LinkedList listIterator()

The Java.util.LinkedList.listIterator() method returns a list-iterator containing the same elements as that of the LinkedList and in the same sequence starting from a specific position or index number. The position or index is passed as a parameter to the function.

Syntax

ListIterator new_list = LinkedList.listIterator(int index);

Parameter: The parameter is an integer number that indicates the element from which ListIterator begins functioning and returning values.

Return value: The method returns a sub list of the original starting from the specified index.

Example

Code: 

import java.io.*;
import java.util.LinkedList;
import java.util.ListIterator;
  
public class LinkedListTest {
    public static void main(String args[])
    {
       //creating a demo list
        LinkedList<String> demoList = new LinkedList<String>();
  
        // Using add() method to add elements in the demoList 
        demoList .add("Coding");
        demoList .add("Ninjas");
        demoList .add("Studio");
        demoList .add("10");
        demoList .add("20");
  
        // Displaying the linkedlist
        System.out.println("LinkedList items:" + list);
          
        // Setting the ListIterator at a specified index
        ListIterator listIter = demoList .listIterator(2);
  
        // Iterating through the new list from the position
        System.out.println("The list using listIterator is as follows:");
        while(listIter.hasNext()){
           System.out.println(listIter.next());
        }
    }
}

Output:

LinkedList items:[Coding, Ninjas, Studio, 10, 20]
The list is as follows:
Studio
10
20

 

Frequently asked question

1). listIterator is in which java package?

Ans: listIterator is in java.util package

 

2). What does the parameter passed to listIterator function indicate?

Ans: The parameter passed to listIterator indicates the starting position from where the iterator starts operating.

 

3). What is the syntax for the listIterator function?

Ans: ListIterator new_list = LinkedList.listIterator(int index);

 

Key takeaways

  • The Java.util.LinkedList.listIterator() method returns a list-iterator containing the same elements as that of the LinkedList and in the same sequence starting from a specific position or index number. The position or index is passed as a parameter to the function.
  • The index parameter is an integer number that indicates the element from which ListIterator begins functioning and returning values..
  • The method returns a sub list of the original starting from the specified index.

 

Happy learning!!

 

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think