ArrayList in Java with Examples

We are already aware that in Java, array size are of fixed-size. Meaning that we have to assign a size to an array, which we cannot increase or decrease.Therefore to change the size, we have to make a new array and copy the data that we want – which is inefficient and a painful technique.

To overcome this we can implement Arraylist in Java.

For more updates do like our Facebook page.

What is an ArrayList?
In essence, an ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Array lists
would increase in size when objects are added and shrinks as objects are removed similar to Dictionary object in VBscript.

We use ArrayList incase we are not sure on the upper bound on the number of elements. On the other side, a simple Array in java is a static data structure,
because the initial size of array cannot be changed, meaning it can be used only when the data has a known number of elements.

How to use it?
By calling the ArrayList constructor.

  1. ArrayList( ) — An Array without any initial capacity
  2. ArrayList(Collection c) — It creates a list containing the elements of the specified collection
  3. ArrayList(int capacity)–An Array with initial capacity

Syntax:-
ArrayList Learn= new ArrayList();
ArrayList Learn= new ArrayList(10);

To create an array list in Java, we need declare an ArrayList variable (Learn) and call the ArrayList constructor to instantiate an ArrayList object and
assign it to the variable. We have taken 10 as the initial size of an Arraylist.

Arraylist Methods in Java
Methods in ArrayList


Adding Elements to Arraylist:-
We need to use the Add() method to add object(element) to the array list.

Learn.Add("Selenium");


Note:-
Incase we have specified a type of an Arraylist,the objects that we add via the add method must be of the correct type.

Arraylist of String type dont accept Int in java
Adding Int element to Arraylist of String Type


We can insert an object at a specific position in the list by listing the position in the add method

Learn.Add(1,"UFT"); //Thus index 1 have UFT in it .


Updating value of ArrayList:-
We need to use Set method for the same,

Learn.Add(0,"Java")
Learn.Set(0,"JavaScript")// we updated the index(0) to contain the value of "JavaScript" instead of "Java"


Deleting Elements in ArrayList:-
To remove all the elements, use the clear method:
Learn.clear();
To remove a specific element based on the index number, use the remove method:
Learn.remove(0);

Note:-
Incase we don’t know the index of the object we want to remove, we can remove it by using the reference name,

Learn.remove(Selenium); //instead of using Learn.remove(0);


Count of elements in Arraylist:-
We call the size() method on the ArrayList instance. This returns the number of elements in the collection.

int iCnt = Learn.Size();


How to fetch element value from a given index:-
We use get() method, which receives the index of the element to get.

String value = Learn.get(0);

we can loop and iterate to get the value in each element of the arraylist.

Element sorting:-
Collections.sort. We can sort an ArrayList with this method.  This method sorts in ascending (low to high) order.


Collections.sort(Learn);



CODE:-
Example1:-

  • Creating ArrayList by using Add Method
  • Displaying ArrayList
  • Further using Index to Add elements into ArrayList 
  • Using Get Method
  • And getting the Size of Arraylist. 

import java.util.ArrayList;

public class LearnArrayList {
public static void main(String[] args) {
//Creating Arraylist
ArrayList arrList = new ArrayList();
//Adding Elements into Arraylist
arrList.add("Selenium");
arrList.add("UFT");
arrList.add("JAVA");
arrList.add("VBSCRIPT");
//Taking output of Arraylist
System.out.println("Values in ArrayList ="+arrList);
//Using Index to add Elements to ArrayList
arrList.add(1,"QTP");
//Get method to fetch value at given index
System.out.println("New Value at Index '1' ="+arrList.get(1));
//Size of ArrayList
System.out.println("Size = "+arrList.size());
}
}

#Output:-
Values in ArrayList =[Selenium, UFT, JAVA, VBSCRIPT]
New Value at Index '1' =QTP
Size = 5


Example2:-

  • Removing particular Element from Arraylist –Remove method 
  • Deleting all the elements of Arraylist–Clear method 

import java.util.ArrayList;

public class LearnArrayList {
public static void main(String[] args) {
//Creating String ArrayList of initial capacity of 3
ArrayList<String> arrString = new ArrayList<String>(2);
//add 4 elements
arrString.add("JAVASCRIPT");
arrString.add("HTML");
arrString.add("CSS");
arrString.add("ANGULAR");
System.out.println("ArrayList="+arrString);
//Remove particular element
arrString.remove("CSS");
arrString.remove(0);
System.out.println("New List after Remove="+arrString);
//Clear the list
arrString.clear();
System.out.println("Empty List"+arrString);
}
}

#Output:-
ArrayList=[JAVASCRIPT, HTML, CSS, ANGULAR]
New List after Remove=[HTML, ANGULAR]
Empty List[]


Example3:-

  • Shuffle Arraylist elements
  • Sort Arraylist
  • Convert Arraylist into Array

import java.util.ArrayList;

public class LearnArrayList {
public static void main(String[] args) {
//Creating String ArrayList of initial capacity of 3
ArrayList<String> arrString = new ArrayList<String>(2);
//add 4 elements
arrString.add("JAVASCRIPT");
arrString.add("HTML");
arrString.add("CSS");
arrString.add("ANGULAR");
System.out.println("Original ArrayList="+arrString);
//Shuffle Arraylist
Collections.shuffle(arrString);
System.out.println("Arraylist after shuffle="+arrString);
//Sort Arraylist
Collections.sort(arrString);
System.out.println("Sorted Arraylist"+arrString);
//Creating an Array from ArrayList
String[] arrLearn = new String[arrString.size()];
//Using toarray method
arrString.toArray(arrLearn);
System.out.println("Newly Created Array=");
for(int iCnt =0;iCnt<arrLearn.length;iCnt++)
{
System.out.println(arrLearn[iCnt]);
}
}
}

#Output:-
Original ArrayList=[JAVASCRIPT, HTML, CSS, ANGULAR]
Arraylist after shuffle=[ANGULAR, HTML, CSS, JAVASCRIPT]
Sorted Arraylist[ANGULAR, CSS, HTML, JAVASCRIPT]
Newly Created Array=
ANGULAR
CSS
HTML
JAVASCRIPT