The ListIterator interface extends the Iterator interface to support bi-directional access as well as adding or removing or changing elements in the underlying collection.
A ListIterator has no current element. Its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().
In a list of length n, there are n+1 valid index values, from 0 to n, inclusive.
An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.
NOTE: A ListIterator is implemented only on List interface through different ways.
ListIterator interface is different from Iterator by following ways :
1. In ListIterator interface process of cycling is done in two-way direction whereas in Iterator this process is one-way.
2. ListIterator interface supports more methods than the Iterator interface.
Following are the list of methods available in ListIterator interface:
Example:
package com.sample.javase.testing;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ListInterfaceExample {
/**
* @param args
*/
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("servlets");
list.add("jsp");
list.add("jdbc");
// We can print list as below or we can iterate it using iterator.
System.out.println(list);
// We can access elements of listr using iterator
for (Iterator<String> iterator2 = list.iterator(); iterator2.hasNext();) {
String string = (String) iterator2.next();
System.out.println(string);
}
ListIterator<String> listIterator = list.listIterator();
listIterator.add("webservices");
if (listIterator.hasPrevious()) {
System.out.println("using list iterator"+ listIterator.previous());
}
}
}
A ListIterator has no current element. Its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().
In a list of length n, there are n+1 valid index values, from 0 to n, inclusive.
An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.
NOTE: A ListIterator is implemented only on List interface through different ways.
ListIterator interface is different from Iterator by following ways :
1. In ListIterator interface process of cycling is done in two-way direction whereas in Iterator this process is one-way.
2. ListIterator interface supports more methods than the Iterator interface.
Following are the list of methods available in ListIterator interface:
Example:
package com.sample.javase.testing;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ListInterfaceExample {
/**
* @param args
*/
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("servlets");
list.add("jsp");
list.add("jdbc");
// We can print list as below or we can iterate it using iterator.
System.out.println(list);
// We can access elements of listr using iterator
for (Iterator<String> iterator2 = list.iterator(); iterator2.hasNext();) {
String string = (String) iterator2.next();
System.out.println(string);
}
ListIterator<String> listIterator = list.listIterator();
listIterator.add("webservices");
if (listIterator.hasPrevious()) {
System.out.println("using list iterator"+ listIterator.previous());
}
}
}
No comments:
Post a Comment