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 (42 votes)
Share this
Anonymous's picture

some more examples........

some more examples........
Anonymous's picture

i want list implementation

i want list implementation calsses example more
Anonymous's picture

Hi Guys, here are some more

Hi Guys, here are some more examples of arraylist 10 Arraylist Java Example
Anonymous's picture

more examples needed

more examples needed
Anonymous's picture

Thanks for your simple

Thanks for your simple examples. it helped me a lot
Anonymous's picture

Its vry helpful

Its vry helpful
Anonymous's picture

it was too helpful. Thanks a

it was too helpful. Thanks a lot.
Anonymous's picture

Code more comment less okay

Code more comment less okay
Admin's picture

This example is meant for the

This example is meant for the one who is learning Java, so commenting would help them learn it better.
Anonymous's picture

yes i did..

yes i did..
Anonymous's picture

Isn't it nice to learn by

Isn't it nice to learn by that way????
Anonymous's picture

Thanks for Ur Simple

Thanks for Ur Simple Example... Its very useful for me..................
Anonymous's picture

thanks for the super examples

thanks for the super examples
Anonymous's picture

I want to see example with

I want to see example with multiple dynamic ArrayList with asociative keys fill and print. Does it hurts You hah?
Anonymous's picture

its useful..thanks

its useful..thanks
Anonymous's picture

its helpful....but it might

its helpful....but it might be more helpful if an example of "for" loop is given to enter elments of an arraylist..
Anonymous's picture

it was helpful

it was helpful
Anonymous's picture

thanks for a helpfull

thanks for a helpfull example
Anonymous's picture

how do you write the

how do you write the contains() method?
Anonymous's picture

these examples are useless.i

these examples are useless.i need advanced operations
Anonymous's picture

This is VERY helpful. Thank

This is VERY helpful. Thank you so much - computer science student
Anonymous's picture

Hi can any one help.. i

Hi can any one help.. i want to create an object array of the same length as the resultset(say some 5) has columns and then get... into the array indexes and then add that array to the list. regards, asha
Anonymous's picture

thanks guys who di=one it

thanks guys who di=one it really help me in improving my java programming side
Anonymous's picture

gud one whosoever has written

gud one whosoever has written it.
Anonymous's picture

This one is great

This one is great
Anonymous's picture

Amazing

Amazing
Anonymous's picture

I have a list containing

I have a list containing employee object , i want to check whether new eployee what i am adding is already there or not based on employeeId, if it is there i want to fetch that id.any solution??? thanx
Anonymous's picture

You can use contains() method

You can use contains() method of java arraylist to check whether a particular employee object already there or not but if you want to check based on id you need to iterate the list.
Anonymous's picture

waste

waste
Anonymous's picture

THANKS

THANKS
Anonymous's picture

Thank you very much. Good

Thank you very much. Good job!
Anonymous's picture

thanks dude's

thanks dude's
Anonymous's picture

Really helpful.. More can

Really helpful.. More can also be found here about ArrayList.. http://javarajni.blogspot.com/p/arraylist.html
Anonymous's picture

super

super
Anonymous's picture

super

super
Anonymous's picture

fantastic

fantastic
Anonymous's picture

awesome :))

awesome :))
Anonymous's picture

really helpfull

really helpfull

Post new comment