Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Vector removeAll() Method in Java
Next article icon

Vector listIterator() method in Java with Examples

Last Updated : 02 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

java.util.Vector.listIterator()

This method returns a list iterator over the elements of a Vector object in proper sequence. It is bidirectional, so both forward and backward traversal is possible, using next() and previous() respectively. The iterator thus returned is fail-fast. This means that structurally modifying the vector after the iterator is created, in any way except through the iterator’s own remove or add methods (using Vector.add(), for example), will cause iterator to throw ConcurrentModificationException.

Syntax:

  public ListIterator listIterator()  

Parameters: This method accepts no input arguments.

Return Value: This method returns a ListIterator object which can be used to traverse the Vector object.

Example 1: To demonstrate forward and backward traversal using listIterator().




// Java code to illustrate listIterator()
  
import java.util.Vector;
import java.util.ListIterator;
  
public class GFG {
    public static void main(String[] args)
    {
        // Declare empty vector
        Vector<String> vt = new Vector<String>();
  
        vt.add("Geeks");
        vt.add("for");
        vt.add("Geeks");
        vt.add("2019");
        vt.add("AComputerSciencePortalForGeeks");
  
        // Declare list iterator
        ListIterator listItr = vt.listIterator();
  
        // Forward iterations
        System.out.println("Forward Traversal:");
        while (listItr.hasNext()) {
            System.out.println(listItr.next());
        }
  
        // Backward iterations
        System.out.println("\nBackward Traversal:");
        while (listItr.hasPrevious()) {
            System.out.println(listItr.previous());
        }
    }
}
 
 
Output:
  Forward Traversal:  Geeks  for  Geeks  2019  AComputerSciencePortalForGeeks    Backward Traversal:  AComputerSciencePortalForGeeks  2019  Geeks  for  Geeks  

java.util.Vector.listIterator(int index)

This method is used to return a list iterator by specifying starting index. Also bidirectional and fail-fast.

Syntax:

  public ListIterator listIterator(int index)  

Parameters: The parameter index is an integer type value that specifies the first element to be returned from the list iterator (by a call to next()).

Return Value: This method returns a ListIterator object which can be used to traverse the Vector object.

Exception: This method throws IndexOutOfBoundsException, if the index is out of range (index < 0 or index > size())

Example 2: To demonstrate listIterator(int index).




// Java code to illustrate listIterator(int index)
  
import java.util.Vector;
import java.util.ListIterator;
  
public class GFG {
    public static void main(String[] args)
    {
        // Declare empty vector
        Vector<String> vt = new Vector<String>();
  
        vt.add("Geeks");
        vt.add("for");
        vt.add("Geeks");
  
        // Declare list iterator
        ListIterator listItr = vt.listIterator(1);
        // traversal
        while (listItr.hasNext()) {
            System.out.println(listItr.next());
        }
    }
}
 
 
Output:
  for  Geeks  

Example 3: To demonstrate IndexOutOfBoundsException thrown by listIterator(int index).




// Java code to illustrate IndexOutOfBoundsException
// thrown by listIterator(int index)
  
import java.util.Vector;
import java.util.ListIterator;
  
public class GFG {
    public static void main(String[] args)
    {
        // Declare empty vector
        Vector<String> vt
            = new Vector<String>();
  
        vt.add("Geeks");
        vt.add("for");
        vt.add("Geeks");
  
        // Declare list iterator at starting
        // index greater than vector size
        try {
            ListIterator listItr
                = vt.listIterator(5);
        }
        catch (IndexOutOfBoundsException e) {
            // Exception handling
            System.out.println("Exception: " + e);
        }
    }
}
 
 
Output:
  Exception: java.lang.IndexOutOfBoundsException: Index: 5  

Example 4: To demonstrate ConcurrentModificationException thrown by ListIterator object when Vector object is modified after creating list iterator to it.




// Java code to illustrate ConcurrentModificationException
// thrown by ListIterator object
  
import java.util.ConcurrentModificationException;
import java.util.Vector;
import java.util.ListIterator;
  
public class GFG {
    public static void main(String[] args)
    {
        // Declare empty vector
        Vector<String> vt = new Vector<String>();
  
        vt.add("Geeks");
        vt.add("for");
  
        // Declare list iterator
        ListIterator listItr = vt.listIterator();
  
        // modify vector after creating list iterator
        vt.add("Geeks");
  
        try {
            // Exception thrown here
            System.out.println(listItr.next());
        }
        catch (ConcurrentModificationException e) {
            // Exception handling
            System.out.println("Exception: " + e);
        }
    }
}
 
 
Output:
  Exception: java.util.ConcurrentModificationException  

Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html#listIterator–



Next Article
Vector removeAll() Method in Java

S

sreeragiyer
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2018
Practice Tags :
  • Java

Similar Reads

  • Java Vector elementAt() Method
    In Java, the elementAt() method is used to fetch or retrieve an element at a specific index from a Vector. It allows to access elements in the vector by providing the index, which is zero-based. Example 1: Here, we use the elementAt() method to retrieve an element at a specified index with an Intege
    2 min read
  • Java Vector elements() Method
    In Java, the elements() method is used to return an enumeration of the elements in the Vector. This method allows you to iterate over the elements of a Vector using an Enumeration, which provides methods for checking if there are more elements and retrieving the next element. Example 1: Below is the
    2 min read
  • Vector ensureCapacity() method in Java with Example
    The ensureCapacity() method of Java.util.Vector class increases the capacity of this Vector instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. Syntax: public void ensureCapacity(int minCapacity) Parameters: This method takes
    2 min read
  • Vector equals() Method in Java
    The java.util.vector.equals(Object obj) method of Vector class in Java is used verify the equality of an Object with a vector and compare them. The list returns true only if both Vector contains same elements with same order. Syntax: first_vector.equals(second_vector) Parameters: This method accepts
    2 min read
  • Vector firstElement() Method in Java
    The java.util.vector.firstElement() method in Java is used to retrieve or fetch the first element of the Vector. It returns the element present at the 0th index of the vector Syntax: Vector.firstElement() Parameters: The method does not take any parameter. Return Value: The method returns the first
    2 min read
  • Vector forEach() method in Java
    The forEach() method of Vector is used to perform a given action for every element of the Iterable of Vector until all elements have been Processed by the method or an exception occurs. The operations are performed in the order of iteration if the order is specified by the method. Exceptions thrown
    3 min read
  • Vector hashCode() Method in Java
    The java.util.vector.hashCode() method in Java is used to get the hashcode value of this vector. Syntax: Vector.hashCode() Parameters: The method does not take any parameter. Return Value: The method returns hash code value of this Vector which is of Integer type. Below programs illustrate the Java.
    2 min read
  • Vector indexOf() Method in Java
    The java.util.vector.indexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the vector does not contain the element. Syntax:
    3 min read
  • Vector insertElementAt() Method in Java with Examples
    insertElementAt() method of Vector class present inside java.util package is used to insert a particular element at the specified index of the Vector. Both the element and the position are passed as the parameters. If an element is inserted at a specified index, then all the elements are pushed upwa
    3 min read
  • Vector isEmpty() Method in Java
    The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences