Java ArrayList class in collections

Java ArrayList class in collections



--  ArrayList class uses a dynamic array for storing the elements. It inherits AbstractList class and implements List interface in java. 

--  ArrayList class can allows random access because array works at  the index basis. 

--  ArrayList is non synchronized class in java.

--  ArrayList class maintains insertion order to data

--  ArrayList can contain duplicate elements.

--  manipulation is slow in ArrayList class because a lot of shifting 
    needs to operation with data.


  ArrayList class have three Constructor. 

  1) ArrayList ( )

  2) ArrayList ( Collection c )

  3) ArrayList ( int capacity )

 

 Some Methods of  ArrayList class

  1) void add ( int index, Object element )

   -   insert the specified element at the specified position index in a list.

  2) boolean addAll ( Collection c )

   -  used to append all of the elements in the specified collection to the
      end of this list

  3) void clear ( )
   
   -  remove all of the elements from this list.

  4) void trimToSize ( )
   
   -  trim the capacity of this ArrayList instance to be the list's current 
      size.
 
  5) int lastIndexOf ( Object o )
   
   -  It is used to return the index in this list of the last occurrence of 
      the specified element in list.

  6) Object [ ] toArray ( )

   -  It is used to return an array containing all of the elements in 
      this list in the correct order.


Declaration of ArrayList

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable 

Example of Arraylist. 

 import java.util.*;   

 class JavaArrayList {   

    public static void main ( String args [ ] ) {   

       ArrayList<String> arrayList = new ArrayList < String > ( );

       arrayList.add ("Nirav");

       arrayList.add ("Patel");   

       arrayList.add ("On");   

       arrayList.add ("Java");

       Iterator data = arrayList.iterator ( );   

       while ( data.hasNext ( ) ) {   

           System.out.println ( data.next ( ) );   

       }   

   }   

output :  
              Nirav
              Patel
              On
              Java

 

Comments

Popular posts from this blog

Java LinkedList class in Collection

Constructors in Java