Get ArrayList of Int Array in Java

Mohammad Irfan Oct 17, 2021 Sep 13, 2021
  1. Creating ArrayList of Int Type
  2. Adding Integer Element to an Index in ArrayList
  3. Accessing ArrayList Element With the Index
  4. ArrayList of Int Arrays
  5. Accessing Int Array Element From ArrayList
  6. ArrayList of ArrayLists
  7. Summary
Get ArrayList of Int Array in Java

This tutorial introduces how to get ArrayList of ints in Java and lists some example codes to understand the topic.

An ArrayList is a dynamic or resizable array. It is part of the Collection Framework in Java. ArrayList is used to overcome the problem of the fixed size of normal arrays. In this tutorial, we will learn how to create ArrayList of ints.

Creating ArrayList of Int Type

ArrayList or any other collection cannot store primitive data types such as int. If we write the code shown below, then we will get a compilation error.

public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<int> arrList;
	}
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Syntax error, insert "Dimensions" to complete ReferenceType
	at snippet.Demo.main(Demo.java:7)

Instead, we use wrapper classes to store primitives in ArrayList. Each primitive data type has a corresponding wrapper class that represents an object for the same type. For int data type, the wrapper class is called Integer. So, to create an ArrayList of ints, we need to use the Integer wrapper class as its type.

import java.util.ArrayList;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<Integer> arrList = new ArrayList<Integer>();
	}
}

We can now add integers to the ArrayList by using the class’s add() method.

import java.util.ArrayList;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<Integer> arrList = new ArrayList<Integer>();
		arrList.add(5);
		arrList.add(7);
		arrList.add(12);
	}
}

ArrayList, just like normal arrays, follow zero-based indexing. We can specify the index where we want to add an object in the add() method. The element present at that index and all elements to its right will shift one place to the right.

Adding Integer Element to an Index in ArrayList

import java.util.ArrayList;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<Integer> arrList = new ArrayList<Integer>();
		arrList.add(5);
		arrList.add(7);
		arrList.add(12);
		arrList.add(1, 199);//Inserting 199 at index 1.
	}
}

Accessing ArrayList Element With the Index

We can fetch individual ArrayList items by using their indices. Pass the index value to the get() method to fetch the required element.

import java.util.ArrayList;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<Integer> arrList = new ArrayList<Integer>();
		arrList.add(5);
		arrList.add(7);
		arrList.add(12);
		arrList.add(1, 199);//Inserting 199 at index 1.
		System.out.println("Element at index 1: " + arrList.get(1));
		System.out.println("Element at index 2: " + arrList.get(2));
	}
}

Output:

Element at index 1: 199
Element at index 2: 7

We can also print the entire ArrayList by using a single print statement.

import java.util.ArrayList;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<Integer> arrList = new ArrayList<Integer>();
		arrList.add(5);
		arrList.add(7);
		arrList.add(12);
		System.out.println("Printing the ArrayList: " + arrList);
		arrList.add(1, 199);//Inserting 199 at index 1.
		System.out.println("Printing the ArrayList: " + arrList);
	}
}

Output:

Printing the ArrayList: [5, 7, 12]
Printing the ArrayList: [5, 199, 7, 12]

ArrayList of Int Arrays

We can create an ArrayList where each element itself is an array. We use the data type and square brackets to create a new array.

Similarly, we defined the type of the ArrayList by using int[]. We cannot use primitives like int as ArrayList type, but we can use int[]. This is because arrays in Java are objects, not primitives. And an ArrayList can be created by any object type(arrays in our case).

ArrayList<int[]> arrList = new ArrayList<int[]>();

We can perform all the basic operations we discussed above. We need to use the toString() method of Arrays to properly print the array to the console.

import java.util.ArrayList;
import java.util.Arrays;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<int[]> arrList = new ArrayList<int[]>();
		
		int[] arr1 = {2, 4, 6};
		int[] arr2 = {3, 6, 9};
		int[] arr3 = {5, 10, 15};
		
		//Adding int arrays to the ArrayList
		arrList.add(arr1);
		arrList.add(arr2);
		arrList.add(arr3);
		
		//Fetching the array from the List
		int[] arrAtIdx1 = arrList.get(1);
		
		//Printing the fetched array using the toString() method of Arrays
		System.out.println("The Second array in the List is: " + Arrays.toString(arrAtIdx1));
	}
}

Output:

The Second array in the List is: [3, 6, 9]

Accessing Int Array Element From ArrayList

We can also access the individual elements of the int arrays present in the ArrayList. We will use array indices to do this. For example, if we wish to access the second element of the third array, then we will use the following code:

import java.util.ArrayList;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<int[]> arrList = new ArrayList<int[]>();
		int[] arr1 = {2, 4, 6};
		int[] arr2 = {3, 6, 9};
		int[] arr3 = {5, 10, 15};
		
		//Adding int arrays to the ArrayList
		arrList.add(arr1);
		arrList.add(arr2);
		arrList.add(arr3);
		
		//Fetching the second element of the third array
		int[] thirdArr = arrList.get(2);
		int secondElement = thirdArr[1];
		System.out.println("Second Element of the Third Array is: " + secondElement);
	}
}

Output:

Second Element of the Third Array is: 10

However, we need additional code to print the entire ArrayList of arrays.

import java.util.ArrayList;
import java.util.Arrays;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList<int[]> arrList = new ArrayList<int[]>();
		int[] arr1 = {2, 4, 6};
		int[] arr2 = {3, 6, 9};
		int[] arr3 = {5, 10, 15};
		
		//Adding int arrays to the ArrayList
		arrList.add(arr1);
		arrList.add(arr2);
		arrList.add(arr3);
		for(int i = 0; i < arrList.size(); i++)
		{
			int[] currArr = arrList.get(i);
			System.out.println("Array at index " + i + " is: " + Arrays.toString(currArr));
		}
	}
}

Output:

Array at index 0 is: [2, 4, 6]
Array at index 1 is: [3, 6, 9]
Array at index 2 is: [5, 10, 15]

ArrayList of ArrayLists

As discussed above, arrays are of fixed length, but ArrayLists are dynamic. Instead of creating an ArrayList of int arrays, we can create an ArrayList of Integer ArrayLists. This way, we won’t need to worry about running out of space in our array.

ArrayList< ArrayList<Integer> > arrListOfarrLists = new ArrayList< ArrayList<Integer> >();

We can use the add() method and the get() method just like before. However, we require a loop to print each ArrayList element.

import java.util.ArrayList;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList< ArrayList<Integer> > arrListOfarrLists = new ArrayList< ArrayList<Integer> >();
		//Creating individual ArrayLists
		ArrayList<Integer> arrList1 = new ArrayList<>();
		arrList1.add(2);
		arrList1.add(4);
		arrList1.add(6);
		ArrayList<Integer> arrList2 = new ArrayList<>();
		arrList2.add(3);
		arrList2.add(6);
		arrList2.add(9);
		ArrayList<Integer> arrList3 = new ArrayList<>();
		arrList3.add(5);
		arrList3.add(10);
		arrList3.add(15);
		//Adding ArrayLists to the ArrayList
		arrListOfarrLists.add(arrList1);
		arrListOfarrLists.add(arrList2);
		arrListOfarrLists.add(arrList3);
		//Fetching ArrayList
		ArrayList<Integer> listAtIdx1 = arrListOfarrLists.get(1);
		System.out.println("ArrayList present at index 1 is: " + listAtIdx1 +"\n");
		//Printing the entire ArrayList
		for(int i=0; i<arrListOfarrLists.size(); i++)
			System.out.println("ArrayList at index " + i + " is " + arrListOfarrLists.get(i));
	}
}

Output:

ArrayList present at index 1 is: [3, 6, 9]
ArrayList at index 0 is [2, 4, 6]
ArrayList at index 1 is [3, 6, 9]
ArrayList at index 2 is [5, 10, 15]

If you wish to access individual elements of the ArrayList, then use the get() method twice. For example, if you want the second element of the third ArrayList, then use:

import java.util.ArrayList;
public class Demo
{
	public static void main(String[] args)
	{
		ArrayList< ArrayList<Integer> > arrListOfarrLists = new ArrayList< ArrayList<Integer> >();
		//Creating individual ArrayLists
		ArrayList<Integer> arrList1 = new ArrayList<>();
		arrList1.add(2);
		arrList1.add(4);
		arrList1.add(6);
		ArrayList<Integer> arrList2 = new ArrayList<>();
		arrList2.add(3);
		arrList2.add(6);
		arrList2.add(9);
		ArrayList<Integer> arrList3 = new ArrayList<>();
		arrList3.add(5);
		arrList3.add(10);
		arrList3.add(15);
		//Adding ArrayLists to the ArrayList
		arrListOfarrLists.add(arrList1);
		arrListOfarrLists.add(arrList2);
		arrListOfarrLists.add(arrList3);
		//Fetching second element of the third ArrayList
		ArrayList<Integer> thirdList = arrListOfarrLists.get(2);
		int secondElement = thirdList.get(1);
		System.out.print("second element of the third ArrayList is: " + secondElement);
	}
}

Output:

second element of the third ArrayList is: 10

Summary

ArrayList is probably the most commonly used Collection in Java. It is a simple data structure used to store elements of the same type. We cannot create an ArrayList of primitive types like int. We need to use the wrapper classes of these primitives. The ArrayList class provides convenient methods to add and fetch elements from the list. We can also create an ArrayList of arrays or an ArrayList of ArrayLists. They are mostly used to represent data in a 2D matrix format or a tabular format. It is better to use an ArrayList of ArrayLists, as it won’t limit its size.

Related Article - Java ArrayList