Ads Sponsored By Google :)

Monday 21 January 2013

difference|Methods in listiterator and iterator in java|6 collections with example|api|code linkedlist|arraylist|list


iterator in java iterator example in java iterator example iterator example in java collections methods in iterator interface in java listiterator and iterator difference iterator example for arraylist iterator interface in java with example  iterator interface in java 6 iterator interface in java iterator interface type iterator interface api in java iterator interface in java listiterator in java listiterator in java with example listiterator and iterator difference listiterator example in java collections listiterator example listiterator example code in java listiterator example linkedlist listiterator example arraylist listiterator example list listiterator interface api in java java list iterator interface methods in listiterator listiterator interface in java 6

   
   Iterator Interface:
         
          Iterator is one of the predefined interface present in java.util.* package. An object of iterator is always used for extracting or retrieving the values of any collection frame work variable only in forward direction. But in forward direction, But not in backward direction.
        Whenever we create an object of iterator interface, it is by defaulting pointing just before the first element of any collection frame work variable.

 Methods of Iterator Interface :

      1)      Public Boolean hasNext() :
              This method is used for changing whether the collection frame work contains next element. Otherwise it returns false.
     
      2)      Public Object next() :
              This method is used for obtaining or retrieving next element of any collection frame work variable w.r.t  Iterator  interface object provided returns true.

List Iterator Interface :
         ListIterator is one of the predefined sub interface of Iterator interface and it is present in java.util.*. an object of List iterator is always used for extracting or retrieving the elements of any collection frame work variable either forward or in back ward direction i.e; bi-directional we can retrieve.
When we create an object of List Iterator interface, by default it is pointing just before the first interface of any collection frame work variable.

Methods in ListIterator :

1)      Public boolean hasNext() :
            This method returns true provided by collection frame work variable is having next element. Otherwise it returns false.

2)      Public object next() :
           This method is used for obtaining next element of any collection frame work variable w.r.t an object of ListIterator.

3)      Public boolean hasPrevious() :
          This method returns true provided collection frame variable is having previous element otherwise it returns false.

4)      Public object Previous() :
        This method is used for obtaining previous element of any collection frame work variable w.r.t an object of List Iterator provided.

The Following Example Illustrate the Concept of Iterator And ListIterator interfaces :
            import java.util.*;
            Class Itrdemo
          {
              Public static void main(String args[])
            {
              ArrayList al=new ArrayList();
             System.out.println(“Content of al=”+al);
             System.out.println(“is linked list empty =”+al.isEmpty());
             System.out.println(“Size of ll=”+al.size());
             //add the data to LinkedList Object al
              al.add(new Integer(20));
              al.add(new Integer(30));
              al.add(new Integer(40));
            System.out.println(“Content of al”+al);//[20,30,40]
            System.out.println(“is LinkedList empty ?”+al.isEmpty());//false
             // add the element 5 to Linked list at first
             al.addFirst(new Integer(10));
            //add the element 50 to Linked List at Last
            al.addLast(new Integer(50));
            System.out.println(“Content of al=”+al);
            System.out.println(“Size of al=”+al.size());
           //Extracting the data from Linked List
           System.out.println(“Extracting the data from linked list object al by using iterator”);
           Iterator itr=al.iterator();
           int s=0;
           for( ;itr.hasNext(); )
          {
              Object obj=itr.next();
              Integer io=(Integer)obj;
              int  x=io.intValue();
              System.out.println(x);
              s=s+x;
            }
            ListIterator litr=al.listiterator();
           While(litr.hasNext())
            {
               Object obj1=litr.next();
              System.out.println(obj1);
            }
           System.out.println(“Extracting the data from Linked list by using List iterator”);
                 While(litr.hasPrevious())
              {
                 object obj2=litr.Previous();
                 Integer io1=(Integer)obj2;
                 int x1=io1.intValue();
                 System.out.println(x1);
               }
             }
           }
          }
      Data Extraction Interfaces:


          1- D and 2-D Collection Frame work classes are 
  
               To read the values dynamically from keyboard their is a class.
                     
                     Java.util.Scanner or Scanner class

No comments:

Post a Comment

LinkWithin