Google News
logo
Java Array List
ArrayList
1) Introduced in 1.2 version.
2) ArrayList supports dynamic array that can be grow as needed.it can dynamically increase and decrease the size.
3) Duplicate objects are allowed.
4) Null insertion is possible.
5) Heterogeneous objects are allowed.
6) The under laying data structure is growable array.
7) Insertion order is preserved.
Creating ArrayList object:
We can create a ArrayList objects in many different ways as shown below:
ArrayList arl = new ArrayList(); 

The preceding statement constructs an ArrayList with a default initial capacity of 10. We can also mention the capacity at the time of creating ArrayList object as:
ArrayList arl = new ArrayList(101);
The preceding statement constructs an ArrayList with a default initial capacity declared as 101.
ArrayList a1=new ArrayList(collection);

The preceding statement constructs an ArrayList with collection.
Java Non-generic Vs Generic Collection
Java collection framework was non-generic before JDK 1.5. Since 1.5, it is generic.

Java new generic collection allows you to have only one type of object in collection. Now it is type safe so typecasting is not required at run time.

Let's see the old non-generic example of creating java collection.

ArrayList al=new ArrayList();//creating old non-generic arraylist
Let's see the new generic example of creating java collection.

ArrayList al=new ArrayList();//creating new generic arraylist
In generic collection, we specify the type in angular braces. Now ArrayList is forced to have only specified type of objects in it. If you try to add another type of object, it gives compile time error.

For more information of java generics, click here Java Generics Tutorial.
ArrayList program
import java.util.ArrayList; 

import java.util.*; 
class Test 
{ 
public static void main(String[] args) 
{ 
ArrayList al=new ArrayList(); 
System.out.println(al.isEmpty()); 
al.add(10); 
al.add("hi"); 
al.add("abc"); 
System.out.println(al.size()); 
System.out.println("before remove value"+al); 
System.out.println(al.remove(0)); 
System.out.println("after remove value"+al); 
System.out.println("get values"+al.get(0)); 
System.out.println("get index of value"+al.indexOf("hi")); 
System.out.println("searching "+al.contains(10)); 
} 
}
Output :
3
before remove value[10, hi, abc]
10
after remove value[hi, abc]
get valueshi
get index of value0
searching false
ArrayList with generics
import java.util.*; 
class ArrayListDemo 
{ 
public static void main(String[] args) 
{ 
ArrayList al=new ArrayList(); 
al.add(10); 
al.add(20); 
al.add(30); 
al.add(40); 
int sum=0; 
for (Integer a1:a) 
{ 
sum=sum+a1; 
System.out.println(a1); 
} 
System.out.println(sum); 
} 
}
Output :
100
Java ArrayList Example: Book
import java.util.*; 
class Book { 
int id; 
String name,author,publisher; 
int quantity; 
public Book(int id, String name, String author, String publisher, int quantity) { 
this.id = id; 
this.name = name; 
this.author = author; 
this.publisher = publisher; 
this.quantity = quantity; 
} 
} 
public class ArrayListExample { 
public static void main(String[] args) { 
List list=new ArrayList(); 
Book b1=new Book(101,"Let us C","Ramana Reddy","VVRR",8); 
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4); 
Book b3=new Book(103,"Operating System","Galvin","Wiley",6); 
list.add(b1); 
list.add(b2); 
list.add(b3); 
for(Book b:list){ 
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity); 
} 
} 
} 
Output :
101 Let us C Ramana Reddy VVRR 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6