ArrayList in Java: Detailed Topic in Just 5 Minutes

Introduction to ArrayList in Java

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 shrunk to a lower size.
It can hold the 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 increases automatically as and when 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 the ArrayList class is given below:

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 the List interface. The elements of an ArrayList can be directly accessed using get() and set() methods.

NumberMethod NameMethod DescriptionReturnComment1add(Object obj)Adds an element to an ArrayList after the last element or at the current position object obj the remaining size is well within the permissible limit, ArrayList will not grow else it will follow a growing algorithm and increases its size2remove(Object obj)removes the object from the ArrayList or from an index. Size is shrinken3int size()Returns the size of the size
4boolean Contains(Object obj)Returns true if the obj is present inside ArrayList. or else it will return false boolean-true or false

See also  Class Observable 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 below:
Note: uses unchecked or unsafe operations.
Note: Recompile with -Xlint: unchecked for details.
$java -Xmx128M -Xms16M ArrayListTest
The size of the ArrayList is 4
[D, E, M, O]
Removed Element-M
[D, E, O]

Further visit: How To Set Classpath in Java – 7 Important Steps To Follow

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, a 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 a 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

See also  Error 1723 in Java? Here Are Steps How to Resolve.

Share and Enjoy !

Leave a Reply

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