ArrayList in Java with Examples – User friendly Tech help

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.
n
nTo overcome this we can implement Arraylist in Java.

n

For more updates do like our Facebook page.
n
What is an ArrayList?
nIn essence, an ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Array lists
nwould increase in size when objects are added and shrinks as objects are removed similar to Dictionary object in VBscript.
n
nWe 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,
nbecause the initial size of array cannot be changed, meaning it can be used only when the data has a known number of elements.
n
nHow to use it?
nBy calling the ArrayList constructor.

n

    n

  1. ArrayList( ) — An Array without any initial capacity
  2. n

  3. ArrayList(Collection c) — It creates a list containing the elements of the specified collection
  4. n

  5. ArrayList(int capacity)–An Array with initial capacity
  6. n

n

Syntax:-
nArrayList Learn= new ArrayList();
nArrayList Learn= new ArrayList(10);
n
nTo create an array list in Java, we need declare an ArrayList variable (Learn) and call the ArrayList constructor to instantiate an ArrayList object and
nassign it to the variable. We have taken 10 as the initial size of an Arraylist.

n

n

n

n

n

n

n

n

Methods in ArrayList

n

nAdding Elements to Arraylist:-
nWe need to use the Add() method to add object(element) to the array list.
n

n

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

n

n

n

n

n

n

n

n

Adding Int element to Arraylist of String Type

n

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

n

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

n

nUpdating value of ArrayList:-
nWe need to use Set method for the same,

n

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

n

nDeleting Elements in ArrayList:-
nTo remove all the elements, use the clear method:
nLearn.clear();
nTo remove a specific element based on the index number, use the remove method:
nLearn.remove(0);
n
nNote:-
nIncase we don’t know the index of the object we want to remove, we can remove it by using the reference name,

n

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

n

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

n

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

n

String value = Learn.get(0);

n

we can loop and iterate to get the value in each element of the arraylist.
n
nElement sorting:-
nCollections.sort. We can sort an ArrayList with this method.  This method sorts in ascending (low to high) order.

n

n
nCODE:-
nExample1:-

n

    n

  • Creating ArrayList by using Add Method
  • n

  • Displaying ArrayList
  • n

  • Further using Index to Add elements into ArrayList 
  • n

  • Using Get Method
  • n

  • And getting the Size of Arraylist. 
  • n

n

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

n


Example2:-

n

    n

  • Removing particular Element from Arraylist –Remove method 
  • n

  • Deleting all the elements of Arraylist–Clear method 
  • n

n

import java.util.ArrayList;
public class LearnArrayList {
public static void main(String[] args) {
//Creating String ArrayList of initial capacity of 3
ArrayList arrString = new ArrayList(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[]

n


Example3:-

n

    n

  • Shuffle Arraylist elements
  • n

  • Sort Arraylist
  • n

  • Convert Arraylist into Array
  • n

n

import java.util.ArrayList;
public class LearnArrayList {
public static void main(String[] args) {
//Creating String ArrayList of initial capacity of 3
ArrayList arrString = new ArrayList(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
Was this article helpful?
YesNo

Similar Posts