List of Arrays in Java

Mohd Mohtashim Nawaz Feb 11, 2022
  1. What is List in Java
  2. What are Arrays in Java
  3. Difference Between Lists and Arrays in Java
  4. Create a List of Arrays in Java
  5. Conclusion
List of Arrays in Java

Java is a popular and powerful programming language used in multiple domains. Therefore, we often encounter situations where we want to store the data as an ordered collection.

Java provides the List interface to store different data formats as an ordered collection under one name.

The List interface is a parent interface to many child classes. For example, ArrayList, LinkedList, Stack, and Queue implement the List interface.

On the other hand, arrays are another popular method of storing data collection with the same data type. Arrays are simple and easy to handle.

We can easily create arrays of different data types and perform operations using indices. This article will understand the difference between the Lists and arrays in Java and create a list of arrays in Java.

What is List in Java

In Java, the List is an ordered collection of data with the same data type. The Java lists are index-based, where elements can be accessed using indices.

We can perform operations such as insertion, deletion, updating, etc.

The Java lists are not limited to storing primitive data types. We can store objects, arrays, and any other complex data type in a list, and we should note that a list can even store a null element.

Since the List is an interface, we can not directly instantiate an object of type List.

Rather, Java has various Lists types such as ArrayList, LinkedList, etc. These classes implement the List interface and provide the specific functionalities of each type.

We can also create our list type by implementing the List interface and defining its methods.

We can define and instantiate an ArrayList as given below.

List<datatype> nameList = new ArrayList<datatype>();

Note that we can not instantiate an interface, but we can define an object of an interface. Therefore, we need to instantiate a specific list type such as ArrayList, LinkedList, etc.

We can use the different syntax as given below. As the List is the parent of the ArrayList, the List type object can store an ArrayList object.

ArrayList<datatype> nameList = new ArrayList<datatype>();

The syntax above defines an object of ArrayList and instantiates the ArrayList type. We can also define the List and instantiate it later.

What are Arrays in Java

On the other hand, Arrays are very simple and are found in almost all programming languages. We can define the arrays of different data types in Java.

Similar to lists, arrays are also not limited to primitive data types, and we can create arrays with any complex data types.

Arrays can be defined directly and can be instantiated dynamically in Java. Java arrays inherit the object class and implement cloneable and serializable interfaces.

The syntax to declare and instantiate an array in Java is given below.

datatype nameArr [] = new datatype[size];

We can also use a slightly different syntax as given below.

datatype [] nameArr = new datatype[size];

Note that we can define an array and instantiate it later as well.

Difference Between Lists and Arrays in Java

  1. Arrays are static and of fixed size. On the other hand, lists are dynamic and resizeable. Java takes care of resizing the lists as per needs.
  2. While instantiating, we must give the size of the array while lists do not need a fixed size.
  3. In performance, arrays are fast as compared to lists.
  4. We must use a method to get the list elements while array elements can be retrieved using the square brackets.
  5. Arrays can be multi-dimensional, but lists are always single-dimensional. However, we can create nested lists.
  6. A simple assignment operator can not add elements to a list. We have a method to add elements to a list. In arrays, we can use an assignment operator to store elements.

Create a List of Arrays in Java

We can create a list of arrays in Java in the same way as we create the list of any other data type or data structure. However, we can not instantiate a list object; therefore, we shall choose a specific list type for this operation.

While creating a list of arrays, we first define the list by passing the type of data the list holds. We then instantiate the list using the default constructor.

Once the list of arrays is created and instantiated, we can store the arrays into the list by using the add() method provided by the List interface.

We have created a list for storing integer arrays in the following Java program. Note that we have used the Integer object type rather than the primitive integer type.

The reason is that Java converts the primitive data types to object types before storing them in a list.

We have used the ArrayList to instantiate an object. Further, we have created two integer arrays of fixed size and stored integers into them arbitrarily.

You can create arrays of any type and size, but you should remember to change the list definition with your object type.

Finally, we add the arrays to the list and traverse the list and arrays into it using the for loop. Also, note that we have to use the get() method of List to retrieve the list elements, in this case, the array of integers.

The code is given below.

import java.util.*;

public class ListofArrays {
	public static void main(String []args)
	{
		List<Integer[]> list;
		list = new ArrayList<Integer[]>();

		// Let us create 2 arrays of type Integer

		Integer [] arr1 = new Integer[5];
		Integer [] arr2 = new Integer[5];
		//arr1 stores numbers from 0 to 4
		//arr2 stores the numbers from 5 to 9
		for(int i=0;i<5;i++)
		{
			arr1[i] = i;
		}
		for(int i=0;i<5;i++)
		{
			arr2[i] = i+5;
		}
		//adding both arrays to list
		list.add(arr1);
		list.add(arr2);
		//print elements of the list
		for(int i=0;i<list.size();i++)
		{
			Integer [] arr = list.get(i);
			for(int j=0;j<arr.length;j++)
			{
				System.out.print(arr[j]+ " ");
			}
			System.out.println();
		}
	}
}

Output:

0 1 2 3 4
5 6 7 8 9

Since we stored five integers starting from 0 into the first array and the next five integers into the second array, we get the expected output.

We must be careful to define and instantiate the array before adding the array to the list. We can not add an array that has not been instantiated.

However, do not misunderstand the uninstantiated array with the null element. We can still add the null element to the list.

Also, make sure you do not do common mistakes such as instantiating a List object, adding an uninstantiated array to the list of arrays, etc.

Conclusion

We often come across the lists in Java owing to their powerful implementation and the facilities they provide. On the other hand, arrays are probably the most used data structure in programming.

Therefore, we must learn how to create lists of arrays.

Related Article - Java Array

Related Article - Java List