0% found this document useful (0 votes)
5 views5 pages

List It Erator

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

List It Erator

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Limitations:

 By using Enumeration and Iterator we can Move Only towards Forward Direction and
we
can’t Move Backward Direction. That is these are Single Direction Cursors but Not
Bi-
Direction.
 By using Iterator we can Perform Only Read and Remove Operations and we can't
Perform
Addition of New Objects and Replacing Existing Objects.
To Overcome these Limitations we should go for ListIterator.

3) ListIterator:
 ListIterator is the Child Interface of Iterator.

 By using ListIterator we can Move Either to the Forward Direction OR to the


Backward Direction. That is it is a Bi-Directional Cursor.

 By using ListIterator we can Able to Perform Addition of New Objects and


Replacing
existing Objects. In Addition to Read and Remove Operations.

 We can Create ListIterator Object by using listIterator().


public ListIterator listIterator();

Eg:ListIteratorltr = l.listIterator(); //l is Any List Object

Methods:
 ListIterator is the Child Interface of Iterator and Hence All Iterator Methods by
Default
Available to the ListIterator.

ListIterator Defines the following 9 Methods

public boolean hasNext()


public Object next()
public int nextIndex()

public boolean hasPrevious()


public Object previous()
public int previousIndex()

public void remove()


public void set(Object new)
public void add(Object new)

Note : ListIterator we can use only with list .

Example :

package list;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListIterator;
public class ArrayListDemo {

public static void main(String[] args) {

ArrayList al = new ArrayList();


al.add(11);
al.add(22);
al.add("Santosh");
al.add(11);
System.out.println(al);
ListIterator listIterator = al.listIterator();

System.out.println("Forward Direction");

while(listIterator.hasNext()) {
Object next = listIterator.next();
System.out.println(next);
}

System.out.println("Backword Direction");

while (listIterator.hasPrevious()) {
Object previous = listIterator.previous();
System.out.println(previous);
}

}
}

Example :
package list;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListDemo {

public static void main(String[] args) {

ArrayList al = new ArrayList();


al.add(11);
al.add(22);
al.add("Santosh");
al.add(11);
System.out.println(al);
ListIterator listIterator = al.listIterator();

System.out.println("Forward Direction");

while(listIterator.hasNext()) {
Object next = listIterator.next();
System.out.println(next);
System.out.println(listIterator.nextIndex());
}
System.out.println("Backword Direction");

while (listIterator.hasPrevious()) {
Object previous = listIterator.previous();
System.out.println(previous);
System.out.println(listIterator.previousIndex());
}

}
}

Example :

Example :

package list;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListDemo {

public static void main(String[] args) {

ArrayList al = new ArrayList();


al.add(11);
al.add(66);
al.add(99);
al.add("Santosh");
al.add(90);
System.out.println(al);

ListIterator listIterator = al.listIterator();

System.out.println("Forward direction");
while (listIterator.hasNext()) {
Object next = listIterator.next();
System.out.println(next);
if(next.equals(99)) {
listIterator.remove();
}

System.out.println(al);

}
}

Example :
package list;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListDemo {

public static void main(String[] args) {

ArrayList al = new ArrayList();


al.add(11);
al.add(66);
al.add(99);
al.add("Santosh");
al.add(90);
System.out.println(al);

ListIterator listIterator = al.listIterator();

System.out.println("Forward direction");
while (listIterator.hasNext()) {
Object next = listIterator.next();
System.out.println(next);
if(next.equals(99)) {
listIterator.add(333);
}

System.out.println(al);

}
}

Example :

package list;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListDemo {

public static void main(String[] args) {

ArrayList al = new ArrayList();


al.add(11);
al.add(66);
al.add(99);
al.add("Santosh");
al.add(90);
System.out.println(al);
ListIterator listIterator = al.listIterator();

System.out.println("Forward direction");
while (listIterator.hasNext()) {
Object next = listIterator.next();
System.out.println(next);
if(next.equals(99)) {
listIterator.set(333);
}

System.out.println(al);

}
}

You might also like