ArrayList in Java


Prev Tutorial Next Tutorial

ArrayList in Java

ArrayList is a replacement of vector class, It is a new class used to store multiple objects.

In ArrayList the data is organizing in the form of cells. Cell values are storing in heap memory and cell address are storing in associative memory.

arraylist

Points to Remember

  • ArrayList class is not Synchronized.
  • ArrayList class elements can be access randomly.
  • In the ArrayList value will be stored in the same order as inserted.
  • ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
  • ArrayList class can contain duplicate elements.
  • ArrayList allows random access because array works at the index basis.

Note: ArrayList calss contains same methods like vector.

Creating ArrayList is nothing but creating an object of ArrayList class.

Syntax

ArrayList al=new ArrayList();

Arraylist Constructor

ArrayList(): This constructor is used for creating an object of ArrayList class.

Syntax

ArrayList al=new ArrayList()

Advantages of ArrayList

  • ArrayList based applications takes less memory space.
  • Retrieving the data from ArrayList will take less time.
  • Performance of ArrayList based applications is more.

Difference Between Vector and ArrayList

VectorArrayList
1Vector is legacy Collection Framework (old class).ArrayList is new Collection Framework.
2Vector is Synchronized by default.ArrayList is not Synchronized.
3For retrieving elements from Vector class can be use foreach loop, iterator, listiterator and enumeration.For retrieving elements from ArrayList class can be use foreach loop, iterator and listiterator.

Example of ArrayList

import java.util.Arraylist;

class DemoArraylist
{
public static void main(String args[])
{  

ArrayList<Integer> al=new ArrayList<Integer>();  // creating arraylist
al.add(10);
al.add(20);
al.add(30);

Iterator itr=al.iterator();  // getting Iterator from arraylist to traverse elements  
while(itr.hasNext())
{  
System.out.println(itr.next());  
  }  
 }  
}  

Output

10
20
30

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements