Artikel Terbaru

Monday, 6 February 2012

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


Array List in java:-


ArrayList is 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 re sizable array. where as 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 popular collection framework used in every day life of java developer.
In most of the backend programming, HashMap is used to store as temparary cache for either request scope or session scope.HashMap is 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:-
1.Object stored in ArrayList are insertion order
2.ArrayList is used to store the collection of objects with size increasing dynamically
3.ArrayList stores the objects including null values.
4.ArrayList is not synchronized


1. How to create a ArrayList:-
ArrayList has default constructor as well as constructor with integer parameter.
arraylist can be intialized with integer number also.

ArrayList emptylist=new ArrayList(); or
 ArrayList list=new ArrayList(10);
 
The above lines of code create ArrayList object with empty size as well as fixed size of array list.second case, the fixed size is 10

2. How to add Objects to ArrayList.
ArrayList provides add method to add the object to ArrayList
he map.
ArrayList list=new ArrayList();
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");

3. How to show 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 list as it will throws NullPointerException if list is null. add the following code snippet for avoiding NullPointerException in ArrayList
if(list!=null){
add the for loop code here
}

4.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 arry list , as 5 is returned for the above code

5.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 other 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 return -1 if element/object is not avialble in array list.

6. How to Convert ArrayList to array:-
As array list to array is one of common mechanism in day to day programming.

collection API provides arrayList.toArray() method used to convert to 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

7.How to convert array to ArrayList in java:-
to conver the array to arraylist, collection API provides java.util.Arrays class asList() method to be passed to ArrayList constructor
String[] strObjects = {"One1", "two2", "three3", "four4"};
List arrayList= new ArrayList(Arrays.asList(strObjects));

8.How to remove/delete element in list:-
ArrayList has remove method to remove the object from the list.
arrayList.remove("one");

9.How to get the elements in list:-

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

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

10.How to sort objects in ArrayList:-

if we are displaying the objects from the list, ther order is of insertion order onlly.
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 wan 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 other 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.