Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – ArrayList

Declaration

class ArrayList<E>
  • The ArrayList class inherits AbstractList class & implements the List interface.
  • ArrayList allow duplicate elements.
  • Elements insertion order is maintained.
  • The ArrayList contains dynamic arrays.
  • Array that increase or decrease in size automatically according to the need is known as dynamic array.
  • ArrayList is non-synchronized.

List of ArrayList Constructors (CLICK HERE)

List of ArrayList Methods (CLICK HERE)

Non Generic Demo

In non-generic demo, we have used non-generic ArrayList class, constructor & methods. Therefore it can contain any type of elements (heterogeneous elements) as shown in program below.

Example

 import java.util.ArrayList;

 public class ArrayListDemo 
 {
     public static void main(String args[]) 
     {
         ArrayList l = new ArrayList();
         l.add("ABC"); // String added
         l.add(10);    // Integer added
         l.add(null);  // null added
         l.add(5.5);   // Float added
         System.out.println(l);
     }
 }

Output

[ABC, 10, null, 5.5]

If we want ArrayList must contain elements of same type only, then we must use generic ArrayList.

Generic Demo

In the program below we have used generic ArrayList that can contain only String type of elements.
If we tried to insert element other than String, we will get compile time error.

Example – I

 import java.util.ArrayList;

 public class GenericArrayListDemo 
 {
     public static void main(String args[])
     {
         ArrayList<String> aList = new ArrayList<String>();
         aList.add("A");
         aList.add("B");
         aList.add("C");
    //   aList.add(10);  // Integer value is not allowed
		                 // because aList is of type ArrayList<String>
		                 // it can't be used to refer to an
		                 // object of ArrayList<Integer>
         System.out.println(aList);
     }
 }

Output

[A, B, C]

Example – II

 import java.util.ArrayList;

 public class GenericArrayListDemo2 
 {
     public static void main(String args[])
     {
         ArrayList<String> aList = new ArrayList<String>();
         aList.add("A");
         aList.add("B");
         aList.add("C");
         aList.add("D");
         System.out.println(aList);
         aList.set(1,"W");
         System.out.println(aList);
         aList.remove(1);
         System.out.println(aList);
         ArrayList<String> aList2 = new ArrayList<String>();
         aList2.add("X");
         aList2.add("Y");
         aList2.add("Z");
         aList.addAll(aList2);
         System.out.println(aList);
         aList.retainAll(aList2);
         System.out.println(aList);
         System.out.println(aList.get(1));
     }
 }

Output

[A, B, C, D]
[A, W, C, D]
[A, C, D]
[A, C, D, X, Y, Z]
[X, Y, Z]
Y

Example – III

 import java.util.ArrayList;

 class Employee 
 {
     int emp_id;
     String name;
     double salary;
	
     Employee(int i, String s, double d)
     {
         emp_id = i;
         name = s;
         salary = d;
     }
 }

 class ArrayListDemo3
 {
     public static void main(String args[])
     {
         Employee e1 = new Employee(2,"DAVID", 65000.98);
         Employee e2 = new Employee(5,"JAMES", 78000.69);
         Employee e3 = new Employee(1,"RICKY", 45987.45);
         Employee e4 = new Employee(9,"JACK", 130006.68);

         ArrayList<Employee> aList  = new ArrayList<Employee>();
         aList.add(e1);
         aList.add(e2);
         aList.add(e3);
         aList.add(e4);
		
         for(Employee e:aList)
         {
             System.out.println(e.emp_id+"  "+e.name+"  "+e.salary);
         }
     }
 }

Output

2 DAVID 65000.98
5 JAMES 78000.69
1 RICKY 45987.45
9 JACK 130006.68

 

Leave a reply