ArrayList Concept Simplified in Java

digitization, transformation, binary

Share and Enjoy !

Arraylist is an advanced data structure that can be stretched to accommodate additional elements. When we do not have enough elements in the Arraylist, then Arraylist can be shrinked to lower size. It can hold dynamic behavior of the data. So a dynamic array can be put inside ArrayList. We can also change the size of the ArrayList during run time.Every instance of an ArrayList class is allowed to store a set of elements in the list.The capacity of the ArrayList increase automatically as and when the we add elements to the ArrayList. ArrayList extends to AbstractList class and implements the interface such as List,Cloneable and Serializable.

Deceleration of the ArrayList:

ArrayList variableName=new ArrayList();

The constructor of ArrayList class is given below:

Ads code goes here

public ArrayList();
//creates an empty ArrayList. the initial capacity is 10
public ArrayList(Collection c);
//creates an ArrayList with the elements specified in the Collection c passed as an argument.
public ArrayList(int Capacity);
//creates an ArrayList with the capacity specified in the argument.

The ArrayList class inherits the methods from List interface.The elements of an ArrayList can be directly accessed using get() and set() methods.

NumberMethod NameMethod DescriptionReturnComment
1add(Object obj)Adds element to an arraylist after the last element or at the current positionObject objIf the remaining size is well within permissiable limit, arraylist will not grow else it will follow an growing algorithm and increases it size
2remove(Object obj)removes the obejct from the arraylist or from an index.Object objSize is shrinken
3int size()Returns the size of the size
4boolean Contains(Object obj)Returns true if the obj is present inside arralist. or else it will return falseboolean-true or false
READ  How to Create Calculator in Swing in Java

Here is an example of ArrayList::

import java.util.*;
public Class ArryListTest{
public static void main(String[] args){
ArrayList myArrayList=new ArrayList();
//adds "D" to the myArrayList
//adds "E" to the myArrayList
//adds "M" to the myArrayList
//adds "O" to the myArrayList
//find the size of myArrayList
int size=myArrayList.size();
System.out.println("Size of the ArrayList is "+size);
//prints the elements of myArrayList
//removes an element from myArrayList from index-2
Object val=myArrayList.remove(2);
//prints the elements of that is removed
System.out.println("Removed Element-"+val.toString());
//prints the elements of myArrayList
//checks if the myArrayList contains an element "Z"
boolean flag1=myArrayList.contains("Z");
//checks if the myArrayList contains an element "O"
boolean flag2=myArrayList.contains("O");

The output of the program will be as bellow:
Note: uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$java -Xmx128M -Xms16M ArrayListTest
Size of the ArrayList is 4
[D, E, M, O]
Removed Element-M
[D, E, O]
Use of Generics:
from java 5.0 on words each collection class is generalized by providing the type inside the angular bracket(<>). Hence the new definition of ArrayList is given as :

ArrayList<type> variableName=new ArrayList<type>();
ArrayList<String> name=new ArrayList<string>();

In the previous definition ,an user can enter almost anything into an ArrayList but with the use of generics this issue is rectified and the compiler will throw an error if we try to enter different type into another type of ArrayList. With the new definition java knows what kind of values the ArrayList may contain. While fetching an ArrayList, we can also predict what is coming out of an ArrayList.

ArrayList<String> name=new ArrayList<String>();
name.add(new Room());
//compiler will throw an error.
name.add(new String("Test"));
//it is fine as the element is of type String

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *