Java ArrayList Code Examples

JAVA ArrayList is a resizable list of the element as per a specific class or elements. JAVA ArrayList is an implementation of the List Interface. This class is equivalent to Vector.

List Interface provides methods to manipulate the size of the array dynamically. The function which it supports are size, isEmpty, get, set, listIterator, and iterator operations which run as O(1) i.e. in Constant time.

Below is the method to java ArrayList initialize. Its Initialized as below.

import java.util.ArrayList; //import ArrayList Class
ArrayList<String> food = new ArrayList<String>();

Add Method To Insert Element

To add an element in JAVA ArrayList will use add() method. As it is a useful method provided by List Class which is extended by ArrayList in JAVA. As below example shows complete code to get elements added in JAVA ArrayList and print it.

import java.util.ArrayList; //First import the ArrayList fron Util Library
public class Solution{
  public static void main(String[] args){
    ArrayList<String> watches = new ArrayList<String>();
    watches.add("Rolex");
    watches.add("Tisote");
    watches.add("Titan");
    System.out.println(watches);
  }
}

The Above example will add a list of watches in the ArrayList and then all the items of ArrayList are printed at once.

Sort Method

Now let’s focus on a method called sort() in JAVA which is used to Sort the ArrayList in JAVA or any kind on List. Sort method again comes from Class Collections. Let check the example to check the JAVA ArrayList Sort method working code.

import java.util.ArrayList; //First import the ArrayList fron Util Library
import java.util.Collections; //Sort Method is part of Collections Class

public class Solution{
  public static void main(String[] args){
    ArrayList<Integer> nums = new ArrayList<Integer>();
    nums.add(2);
    nums.add(1);
    nums.add(4);
    nums.add(3);
    //Now to sort
    Collections.sort(nums); //sort the number
    for(Integer i:nums){
      System.out.println(i); //prints the number in sorted order of 1,2,3,4.
    }
  }
}

Size Method

Java ArrayList Size method is implemented in List Class which is extended again by ArrayList Class. So Size method gives a number of elements present in ArrayList. Let’s check the example for the Size method below code. Note ArrayList Length is the same as ArrayList Size hence it’s always better to use the Size method to get the size of the List.

import java.util.ArrayList; //First import the ArrayList fron Util Library
import java.util.Collections; //Sort Method is part of Collections Class

public class Solution{
  public static void main(String[] args){
    ArrayList<Integer> nums = new ArrayList<Integer>();
    nums.add(2);
    nums.add(1);
    nums.add(4);
    nums.add(3);
    //Now to Print Number of Element in nums ArrayList
    System.out.println(nums.size()); // Total size of nums is 4 hence 4 is printed 
  }
}

Remove Method

Java ArrayList Remove method is present in List Class. And this Remove method is used to remove a specific element from the ArrayList. Since ArrayList is dynamic List of elements you can remove and add elements and it can re-size automatically.

Let’s check an example code to remove an element from the list. We’ll take the above code only and suppose we want to remove 2 from the list. So all we have to do is add this line.

nums.remove(0); //Since 2 is present at position 0. Hence we provide element
//position to get it deleted.

Contains Method

JAVA ArrayList Contains methods is also part of List Class. This method is used to check if the input element is present in the ArrayList. Let’s take the example and check if the provided elements are present or not using Contains method.

import java.util.ArrayList; //First import the ArrayList fron Util Library
import java.util.Collections; //Sort Method is part of Collections Class

public class Solution{
  public static void main(String[] args){
    ArrayList<Integer> nums = new ArrayList<Integer>();
    nums.add(2);
    nums.add(1);
    nums.add(4);
    nums.add(3);
    //Now to Print Number of Element in nums ArrayList
    System.out.println(nums.contains(1)); // 1 as True is printed.
    System.out.println(nums.contains(5)); // 0 as True is printed.
  }
}

That’s all for today. If you liked our code or tutorials please like our Facebook Page and Follow us on Twitter. Will be posting more tutorial and also putting a lot lesson on various topics.

Further Read:

  1. JAVA HashMap Definitive Guide

Leave a Comment