General

Array list in java

Arraylist Java

General May 10, 2019

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 NameDescription
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

Besant Technologies – Chennai & Bangalore Branch Locations

Besant Technologies - Velachery Branch

Plot No. 119, No.8, 11th Main road, Vijaya nagar,

Velachery, Chennai - 600 042

Tamil Nadu, India

Landmark - Reliance Digital Opposite Street

Besant Technologies - Tambaram Branch

No.2A, 1st Floor, Duraisami Reddy Street,

West Tambaram, Chennai - 600 045

Tamil Nadu, India

Landmark - Near By Passport Seva

Besant Technologies - OMR Branch

No. 5/318, 2nd Floor, Sri Sowdeswari Nagar,

OMR, Okkiyam Thoraipakkam, Chennai - 600 097

Tamil Nadu, India

Landmark - Behind Okkiyampet Bus Stop,

Besant Technologies - Porur Branch

No. 180/84, 1st Floor, Karnataka Bank Building,

Trunk Road, Porur, Chennai - 600 116

Tamil Nadu, India

Landmark - Opposite to Gopalakrishna Theatre

Besant Technologies - Anna Nagar Branch

Plot No:1371, 28th street kambar colony,

I Block, Anna Nagar, Chennai - 600 040

Tamil Nadu, India

Landmark - Behind Reliance Fresh

Besant Technologies - T.Nagar Branch

Old No:146/2- New No: 48,

Habibullah Road,T.Nagar, Chennai - 600 017

Tamil Nadu, India

Landmark - Opposite to SGS Sabha

Besant Technologies - Thiruvanmiyur Branch

22/67, 1st Floor, North mada street, Kamaraj Nagar

Thiruvanmiyur, Chennai 600041

Tamil Nadu, India

Landmark - Above Thiruvanmiyur ICICI Bank

Besant Technologies - Siruseri Branch

No. 4/76, Ambedkar Street, OMR Road, Egatoor, Navallur,

Siruseri, Chennai 600130

Tamil Nadu, India

Landmark - Near Navallur Toll Gate, Next to Yamaha Showroom

Besant Technologies - Maraimalai Nagar Branch

No.37, Ground Floor, Thiruvalluvar Salai,

Maraimalai Nagar,Chennai 603209

Tamil Nadu, India

Landmark - Near to Maraimalai Nagar Arch

Besant Technologies - BTM Layout Branch

No 2, Ground floor, 29th Main Road,

Kuvempu Nagar,BTM Layout 2nd Coming from Silkboard,

AXA company signal, Stage, Bangalore - 560 076

Karnataka, India

Landmark - Next to OI Play School

Besant Technologies - Marathahalli Branch

No. 43/2, 2nd Floor, VMR Arcade,

Varthur Main Road, Silver Springs Layout,

Munnekollal, Marathahalli, Bengaluru - 560037

Karnataka, India

Landmark - Near Kundalahalli Gate Signal

Besant Technologies - Rajaji Nagar Branch

No. 309/43, JRS Ecstasy, First Floor,

59th Cross, 3rd Block, Bashyam Circle,

Rajaji Nagar, Bangalore - 560 010

Karnataka, India

Landmark - Near Bashyam Circle

Besant Technologies - Jaya Nagar Branch

No. 2nd Floor,1575,11th Main Road,

4th T-Block, Pattabhirama Nagar,

Jaya Nagar, Bangalore - 560 041

Karnataka, India

Landmark - Opposite to Shanthi Nursing Home

Besant Technologies - Kalyan Nagar Branch

No.513, 4th Cross Rd

2nd Block, HRBR Layout,

Kalyan Nagar, Bangalore - 560 043

Karnataka, India

Landmark - Opposite to kalayan nagar Axis Bank

Besant Technologies - Electronic City Branch

No. 7, 3rd Floor, Ganga Enclave,

Neeladri Road, Karuna Nagar, Doddathoguru Village,

Electronics City Phase 1, Electronic City,

Bangalore - 560100, karnataka, India

Landmark - Adjacent to HDFC Bank and State Bank Of India

Besant Technologies - Indira Nagar Branch

No.54, 1st Floor,

5th Main kodihalli,

Bengaluru, Karnataka 560008, India

Landmark - Behind Leela Palace Hotel,

Besant Technologies - HSR Layout Branch

Plot No. 2799 & 2800, 27th Main,

1st Sector, HSR Layout,

Bengaluru, Karnataka 560102, India

Besant Technologies - Hebbal Branch

No.29, 2nd Floor, SN Complex,

14th Main Road, E-Block Extention, Sahakara Nagar,

Bengaluru, Karnataka -560092, India

Scroll Up