Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java Iterator
3.
LinkedList listIterator()
3.1.
Example
4.
Frequently Asked Questions
4.1.
listIterator is in which java package?
4.2.
What does the parameter passed to listIterator function indicate?
4.3.
What is the syntax for the listIterator function?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

LinkedList listIterator() Method in Java

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Linked List

Introduction

Linked Lists are generic data types and one of the most elementary Data Structures having a wide range of applications across various fields. Traversing a linked list involves iterating through the list starting from the head pointer to the next till one reaches the end. It operates in a chain-like fashion.

Java Iterator

Java iterator is an object of the Iterator class in java.util 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.

Must Read Difference between ArrayList and LinkedList And Rabin Karp Algorithm

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());
  }
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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: A 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

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 Questions

listIterator is in which java package?

listIterator is in java.util package

What does the parameter passed to listIterator function indicate?

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

What is the syntax for the listIterator function?

ListIterator new_list = LinkedList.listIterator(int index);

 

Conclusion

  • 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.
  • An 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.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy learning!!

Previous article
Generic Linked List in C
Next article
Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
Live masterclass