Learning Java Programming Made Easy
List And List Iterator
List And List Iterator
Java List Interface:
List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface.
Java List Example
- import java.util.*;
- public class ListExample{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Amit");
- al.add("Vijay");
- al.add("Kumar");
- al.add(1,"Sachin");
- System.out.println("Element at 2nd position: "+al.get(2));
- for(String s:al){
- System.out.println(s);
- }
- }
- }
Java ListIterator Interface:
ListIterator Interface is used to traverse the element in backward and forward direction.
Example of ListIterator Interface
- import java.util.*;
- public class TestCollection8{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Amit");
- al.add("Vijay");
- al.add("Kumar");
- al.add(1,"Sachin");
- System.out.println("element at 2nd position: "+al.get(2));
- ListIterator<String> itr=al.listIterator();
- System.out.println("traversing elements in forward direction...");
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- System.out.println("traversing elements in backward direction...");
- while(itr.hasPrevious()){
- System.out.println(itr.previous());
- }
- }
- }
No comments:
Post a Comment
Welcome, happy learning