Sunday, November 29, 2015

ArrayList in java with Examples

ArrayList in Java:

ArrayList is dynamic Data Structure.That means elements can be add or removed from list.But Normal array in java is a  static data structure because once you fixed size of array we can not increase the size of array.

An array is an indexed collection of fixed number of homogeneous data elements.

The main limitations of Object Arrays are

1) Arrays are fixed in size i.e once we created an array there is no chance of increasing or
decreasing it’s size based on our requirement.

2) Arrays can hold only homogeneous data elements.

The underlying data Structure for ArrayList() is resizable Array or “Growable Array”.

  • Duplicate objects are allowed.
  • Insertion order is preserved.
  • Heterogeneous objects are allowed.
  • ‘null’ insertion is possible.
To set up an ArrayList you need to import the package from java.util.*;

Import java.util.ArrayList;



Constructors of Array List


ArrayList l = new ArrayList();

Creates an empty ArrayList object with default intitial capacity 10.
When ever ArrayList reaches its max capacity a new ArrayList Object will be created with new capacity.
capacity=(current capacity*3/2)+1

ArrayList l = new ArrayList(int initial capacity)

Creates an empty ArrayList Object with the specified initial capacity.
ArrayList l = new ArrayList(Collection c)

For inter conversion between collection objects.

Ex:
import java.util.*;
class ArrayListDemo
{
public static void main(String arg[])
{
ArrayList a = new ArrayList();
a.add("A");
a.add(new Integer(10));
a.add("A");
a.add(null);
System.out.println(a);
a.remove(2);
System.out.println(a);
a.add(2,"M");
a.add("N");
System.out.println(a);
}
}
OutPut:
A 10 A null
A 10 null
A 10 M null N

ArrayList and vector classes implement RandomAccess interface. So that we can access any element with the same speed. Hence ArrayList is best suitable if our frequent operation is retrieval operation.Usually the collection objects can be used for data transport purpose and hence every collection implemented class already implemented serializable and cloneable interfaces.ArrayList is the worst choice if u want to perform insertion or deletion in the middle.

Note:- 
ArrayList is not recommended if the frequent operation is insertion or deletion in the middle.To handle this requirement we should go for linked list.

Example of ArrayList Class:

import java.util.*;
class ArrayListEx{
public static void main(String args[])
{
ArrayList<String>al=new ArrayList<String>();//creating arraylist
al.add("lucky");//adding object in arraylist
al.add("sir");
al.add("shiva");
Iterator itr=al.Iterator();//getting iterator from arraylist to traverse elements
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Output:
lucky
sir
shiva




No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...