In the following example we will discuss how can you iterate in list using listIterator (int index) method in List interface in java.
listIterator (int index) method of List Interface in Java
In the following example we will discuss how can you iterate in list using listIterator (int index) method in List interface in java.
Syntax
public ListIterator listIterator(int index)
This method returns a list iterator of the elements in proper sequence started from the position which is specified in the underlying list.
This method works like an Iterator iterator( ) method. This method allows you to traverse the list in either direction - forward or backward direction. The specified index indicates the first element that will be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one. We can modify the list while iterating i.e. we are allowed to replace existing element with the new element and to add new elements in list. In a list of length n there are n+1 valid index values from 0 to n inclusive.
To use a listIterator (int index) in any of the collection, we should follow these steps:
* Find an iterator to the specified position of the collection by calling the collection's
listIterator (int index) method.
* Set a loop that makes a next call, hasNext( ). It returns true if list contains more element.
* Further set a loop that makes a previous call, hasPrevious( ). It returns true if list contains more element.
* You can find element in forward direction from list by calling next( ) method.
* Further you can find element in backward direction from list by calling previous( ) method.
Parameter description
(int index) : This method takes an integer value argument. This input shows position of an element in a list.
Example of listIterator (int index) method
In this example we will show you how does listIterator (int index) method works in List interface. This example will help you to understand how can you iterate in list in forward and backward direction. In this example I take various index value with different ListIterator Object for making example easily understandable. Through this example we will show you the iteration method, display of elements after iteration, replacement of elements and deleting the elements of a list.
Example:-
package Devmanuals.com; import java.util.*; import java.util.ListIterator; public class LISTListIteratorIndex { public static void main(String args[]){ LinkedList ll = new LinkedList (); ll.add("Ramesh"); ll.add("Shyam prasad"); ll.add("Hare ram"); ll.add("Rohan"); ll.add("Mohit"); System.out.println("List is : "+ll); ListIterator li = ll.listIterator(3); System.out.println("Is list has element before the position 3 : "+li.hasPrevious()); System.out.println("//Element in reverse order : "); while (li.hasPrevious()){ System.out.println(li.previous()); } ListIterator li1 = ll.listIterator(3); System.out.println("Is list contains element after the position 3 : "+li1.hasNext()); System.out.println("//Element in forward order : "); while (li1.hasNext()){ System.out.println(li1.next()); } ListIterator li2 = ll.listIterator(0); while (li2.hasNext()){ String s=(String)li2.next(); if(s.equals("Shyam prasad")) li2.set("Shyam"); } System.out.println("After replace name in old list then new list is : "+ll); ListIterator li3 = ll.listIterator(0); while (li3.hasNext()){ String s=(String)li3.next(); if(s.equals("Hare ram")) li3.remove(); } System.out.println("After removing name in old list then new list is : "+ll); } }
Output :
List is : [Ramesh, Shyam prasad, Hare ram, Rohan, Mohit] Is list has element before the position 3 : true //Element in reverse order : Hare ram Shyam prasad Ramesh Is list contains element after the position 3 : true //Element in forward order : Rohan Mohit After replace name in old list then new list is : [Ramesh, Shyam,
Hare ram, Rohan, Mohit] After removing name in old list then new list is : [Ramesh,
Shyam, Rohan, Mohit] |
[ 0 ] Comments