Don't let the Lockdown slow you Down - Enroll Now and Get 3 Course at 24,999/- Only. Explore Now!

General

Array list in java

Arraylist Java

Arraylist Java

  • Java ArrayList class uses a dynamic array for storing the elements.
  • It inherits AbstractList class and implements List interface.
  • It can have duplicates of elements stored and it is not synchronized.(Not Thread safe).

ArrayList

  • Non generic form of ArrayList
  • Generic form of ArrayList

Non generic form of ArrayList

Syntax:

ArrayList al=new ArrayList();

Generic form of ArrayList

Syntax:

ArrayList<DataType> al=new ArrayList< DataType >();  

Example 1: Non generic form

package mypack;
import java.util.ArrayList;
public class ArrayListExample {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub                    
                        ArrayList li=new ArrayList();                      
                        li.add(10)        ;
                        li.add("Besant Technologies");
                        li.add(1000);
                        li.add(2,10.789);// Inserting at 2nd position
                        li.add('c');
                        System.out.println("**********************");
                        System.out.println("Added elements are:");
                        for(Object s: li )
                        {                                    
                           System.out.println(s);
                        }
                        System.out.println("************************");                       
            }
}

Output :

*****************************************

Added elements are:

10

Besant Technologies

10.789

1000

c

*****************************************

Click Here-> Get Prepared for Java Interviews

Example 2: Generic form

package mypack;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExample {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub                    
                        ArrayList<String> list=new ArrayList<String>();//Creating arraylist                  
          list.add("Besant");//Adding object in arraylist
list.add("Technologies");
list.add("HSR Layout");
list.add("Sector-1");
list.add("Bangalore");
                          System.out.println("***********************************");
                          System.out.println("The elements are :");
                          //Traversing list through Iterator 
                          Iterator itr=list.iterator(); 
                          while(itr.hasNext()){ 
                          System.out.println(itr.next()); 
                          } 
                          System.out.println("************************************");
            }
}

Output:

*********************************************************

The elements are :

Besant

Technologies

HSR Layout

Sector-1

Bangalore

*********************************************************

Methods of ArrayList

Method Name Description
boolean add(E e) Appends the element ‘e’ at  end of the list.
void add(int i, E element) Inserts the element at Ith index .
void clear() Removes all the elements from the list.
boolean contains(Object o) Returns true if the list contains the specified object
E get(int index) Returns the element at the specified index in the list.
boolean isEmpty() Checks whether the list is empty.
E remove(int index) Removes the element at the specified index in the list.
E set(int index, E element) Replaces the element at the specified index
int size() Returns the volume (no of elements)in the list.
boolean addAll(Collection<? extends E> c) Appends all of the elements to the end of the list.
boolean removeAll(Collection<?> c) Removes all the elements that are contained in the specified collection from the ArrayList.

Click Here-> Get Java Training with Real-time Projects

Scroll Up
Besant Technologies WhatsApp