Table of Contents
Introduction
The ArrayList class is an implementation of the java.util package and part of the Collections framework. ArrayLists are essentially dynamic arrays of particular datatypes. The difference between the conventional array and array list is that an array list has no size limit while a specific size has to be mentioned when dealing with Arrays. Data manipulation like inserting new elements and deleting elements from the ArrayList can be performed. Therefore, an ArrayList in Java is also more flexible than an Array. It inherits the AbstractList class and implements List Interface.
Features of an ArrayList in Java
- An ArrayList in Java can have duplicate elements.
- ArrayLists are ordered structures. The first element corresponds to the 0th
- ArrayList class is non-synchronized.
- Random access is permitted as the index value defines the position of elements.
- Although both LinkedList and ArrayList are implementations of the List, the linked list is faster than the array list in terms of efficiency. As ArrayLists are index governed rather than reference governed, a lot of shifting has to take place if an element is removed from the list.
Declaration Syntax
public class ArrayList <E> extends AbstractList <E> implements <E>, Random Access, Cloneable, Serializable
Constructors
- ArrayList() – Creates an empty Array list.
- ArrayList (Collection <? extends <E> v) – This constructs an Array list and populates it with the elements of the Collection c passed as parameter.
- ArrayList (int capacity) – This creates an array list with an initial capacity. New elements can be appended after the capacity has been filled as the array list is dynamic in size.
Java Non-generic ArrayList
ArrayList list = new ArrayLIst()
Java Generic ArrayList
This is the commonly used type now.
ArrayList <String> list = new ArrayList <String>(); //This list will contain String type elements in it.
Example
import java.util.*; public class ArrayListDemo { public static void main (String args []) { ArrayList <String> list = new ArrayList <String>(); list.add(“Ram”); list.add(“Varun”); list.add(“Shreya”); list.add(“Agni”); System.out.println(list) } }
0 Comments