Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java ArrayList Example

  1. /*
  2. Java ArrayList example.
  3. This Java ArrayList example describes the basic operations performed on the ArrayList.
  4. */
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8.  
  9. public class ArrayListExample{
  10.    
  11.     public static void main(String args[]){
  12.    
  13.         // constructs a new empty ArrayList    
  14.         ArrayList arrayList = new ArrayList();
  15.        
  16.         /*
  17.         To specify initial capacity, use following constructor.        
  18.         ArrayList ArrayList = new ArrayList(100);
  19.        
  20.         To create ArrayList from Collection use following constructor        
  21.         ArrayList ArrayList = new ArrayList(Collection collection);
  22.        
  23.         In this case the initial capacity would be 110% of the size of the collection.
  24.         */
  25.        
  26.         /*
  27.        
  28.         To add value into ArrayList use add() method.        
  29.         Signature of the add() method is,        
  30.         boolean add(Object object)
  31.        
  32.         The value would be appended to the list.
  33.        
  34.         To insert value into ArrayList at particular index, use        
  35.         void add(int index, Object object)
  36.        
  37.         This method will shifts the subsequent elements of the list.
  38.        
  39.         To append all elements of the collection to existing list, use        
  40.         boolean addAll(Collection c)
  41.        
  42.         To insert all elements of the collection into existing list, use        
  43.         boolean addAll(int index, Collection collection)
  44.        
  45.         To replace particular element in the ArrayList, use        
  46.         Object set(int index, Object value)
  47.        
  48.         It returns the previous element present at the specified index.
  49.        
  50.         */
  51.        
  52.         arrayList.add(new Integer(1)); //adding value to ArrayList
  53.        
  54.         arrayList.add(new Integer(2));
  55.        
  56.         arrayList.add(new Integer(3));
  57.        
  58.         /*
  59.         IMPORTANT : We CAN NOT add primitives to the ArrayList. We have to wrap it
  60.         into one of the wrapper classes before adding.
  61.         */
  62.        
  63.         //get number of keys present in the ArrayList        
  64.         System.out.println("ArrayList contains " + arrayList.size() + " elements.");
  65.        
  66.         /*
  67.        
  68.         To check whether ArrayList is empty or not, use isEmpty() method.
  69.         isEmpty() returns true is ArrayList is empty, otherwise false.
  70.                
  71.         Finding particular value from the ArrayList:        
  72.         ArrayList's contains() method returns boolean depending upon the
  73.         presence of the value in given ArrayList.
  74.        
  75.         Signature of the containsValue method is,        
  76.         boolean contains(Object value)
  77.        
  78.         */
  79.        
  80.         if( arrayList.contains( new Integer(1) ) ){        
  81.             System.out.println("ArrayList contains 1 as value");        
  82.         }else{        
  83.             System.out.println("ArrayList does not contain 1 as value");        
  84.         }
  85.        
  86.         /*
  87.        
  88.         Use get method of ArrayList to get value.
  89.        
  90.         Signature of the get method is,        
  91.         Object get(int index)
  92.        
  93.         */
  94.        
  95.         Integer one = (Integer) arrayList.get(0);        
  96.         System.out.println("Value at 0 index is " + one);
  97.        
  98.         /*
  99.         IMPORTANT:  get method returns Object, so we need to downcast it.
  100.         */
  101.        
  102.         /*
  103.        
  104.         To search element within the ArrayList, use        
  105.         int indexOf(Object element)
  106.        
  107.         it returns the index of first occurrence of the specified element.
  108.        
  109.         To find last occurrence of the specified element, use        
  110.         int lastIndexOf(Object element)
  111.        
  112.         */
  113.        
  114.         System.out.println("Index of first occurrence of 1 is " + arrayList.indexOf(new Integer(1)));
  115.        
  116.         /*
  117.        
  118.         To convert ArrayList to object array, use        
  119.         Object[] toArray() method.
  120.        
  121.         This method returns an Object array containing all elements of the ArrayList
  122.         in the correct order.
  123.        
  124.         */
  125.        
  126.         System.out.println("Converting ArrayList to Object array");
  127.        
  128.         Object[] elements = arrayList.toArray();
  129.        
  130.         for(int i=0; i < elements.length ; i++)        
  131.                System.out.println(elements[i]);
  132.        
  133.         /*
  134.        
  135.         To remove particular element from the ArrayList use,        
  136.         Object remove(int index)
  137.        
  138.         It returns the element that was removed from the list.
  139.        
  140.         To remove multiple elements from the ArrayList use,        
  141.         void removeRange(int fromIndex, int toIndex).
  142.        
  143.         It removes elements from the list whose index is between startIndex (inclusive)
  144.         and toIndex(Exclusive).
  145.        
  146.         To remove all elements of the ArrayList use,        
  147.         void clear().
  148.        
  149.         It removes all elements of the ArrayList.
  150.        
  151.         */
  152.        
  153.         System.out.println("Is 1 removed from the ArrayList ? " + arrayList.remove(new Integer(1)));
  154.    
  155.     }
  156.  
  157. }
  158.  
  159. /*
  160.  
  161. OUTPUT of the above given Java ArrayList Example would be:
  162.  
  163. ArrayList contains 3 key value pair.
  164. ArrayList contains 1 as value
  165. Value at 0 index is 1
  166. Index of first occurrence of 1 is 0
  167. Converting ArrayList to Object array
  168. 1
  169. 2
  170. 3
  171. Is 1 removed from the ArrayList ? true
  172.  
  173. */
Your rating: None Average: 3.9 (47 votes)
Share this