Monday, February 6, 2012

Best examples of using ArrayList Class in java: java array list samples

ArrayList in java

Java ArrayList Examples
ArrayList is the implementation of List interface in java.utils package.ArrayList stores the collection of objects in the order of insertion. ArrayList stores the objects and grows the size of the list dynamically that's why array list is a resizable array. whereas an array is static once assigned the values, the size is fixed. In the real-time programming, ArrayList is used to manipulate the collection of custom objects.

ArrayList is a popular collection framework used in everyday life of Java developer.
In most of the backend programming, HashMap is used to store as a temporary cache for either request scope or session scope.HashMap is a combination of key and values. key and values should be either String, primitives or any custom object. The key stored in the HashMap is based on the hashCode computed for key'object Map is implemented by HashMap in utils package

Important points to remember for ArrayList:-

  • The object stored in ArrayList is insertion order
  • ArrayList is used to store the collection of objects with size increasing dynamically.
  • It is dynamically array, It increases it's size when elements are added, shrinks size when elements are deleted.
  • ArrayList stores the objects including null values.
  • ArrayList is not synchronized
  • These are not sorted

Real-time ArrayList Usage examples.

  • In School management application, You have a list of students stored in Database. To display those students on the User interface, You have to make a call to the database and store these elements in collection List and populate those list elements on UI. here List is used as temporary Object for holding the elements.
  • When you are working on CRUD operations of any database entries, ArrayList is used to store the objects in a temporary cache mechanism

How to create an ArrayList

ArrayList has default constructor as well as a constructor with integer parameter.
ArrayList can be initialized with integer number also.

ArrayList emptylist=new ArrayList(); or
 ArrayList list=new ArrayList(10);
The above lines of code create an ArrayList object with empty size as well as the fixed size of the array list. the second case, the fixed size is 10.
And other ways is ArrayList can also be created and initialized with default values
ArrayList<String> list = new ArrayList<String>() {{
    add("one");
    add("two");
    add("three");
}};

How to add Objects to ArrayList.

ArrayList provides add a method to add the object to ArrayList
Here is an example
ArrayList list=new ArrayList();
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");

3. How to iterate the elements in the ArrayList:-

using for each look, we can iterate each object in the list


for (Object str : list) {
 System.out.print(" " + (String) str);
}

and the output is

One Two Three Four Five
. Always make sure that you check for null check for the list as it will throw NullPointerException if the list is null. add the following code snippet for avoiding NullPointerException in ArrayList

if(list!=null){
add the for loop code here
}

How to find the size of ArrayList?

Java ArrayList API provides size() method to find the number of elements in the HashMap
list.size()
return the size of the list, is returned for the above code

How to check if the object/element is find in ArrayList

arraylist api provides contains() method which returns true if particular object found in the systme
System.out.print(" " + list.contains("One"));
and also another way is to use indexOf() method to get the index of an element in arraylist. This method returns position if the element is found the in
array list else returns -1 if element/object is not avialble in the array list.

6. How to Convert ArrayList to the array:-

As array list to the array is one of a common mechanism in the day to day programming.

collection API provides arrayList.toArray() method used to convert to an array of objects

String[] strArray=(String [])list.toArray(new String[list.size()]);
  for (Object str : strArray) {
   System.out.print(" " + (String) str);
  }

and the output is

One Two Three Four Five

How to convert the array to ArrayList in java:-

to conver the array to ArrayList, collection API provides jaa va.util.Arrays class asList() method to be passed to ArrayList constructor
String[] strObjects = {"One1", "two2", "three3", "four4"};
List arrayList= new ArrayList(Arrays.asList(strObjects));

How to remove/delete an element in the list:-

ArrayList has to use remove method to remove the object from the list.
arrayList.remove("one");

9.How to get the elements in list:-

the list provides get() method to return the return the object found in the list

list.get("one")
if the element is not found, null is returned.

How to sort objects in ArrayList:-

if we are displaying the objects from the list, their order is of insertion order only.
to sort the objects in the ArrayList, collection API provides Collections.sort(List) method to sort in natural order.
Collections.sort(list);
This elements sort in natural order by the hashcode as well equals method default implementation order.

if we want to display the custom order for primitive types or object based on member variables, we have to write our custom comparable java class which is passed to Collections.sort() method as another parameter.


Collections.sort(list,Comparable interface);

I hope you got the basic idea on array list with examples to work.

Please share your comments on this.

Related article


EmoticonEmoticon