ArrayList in Java

Advertisements

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

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements