Updated April 18, 2023
Introduction to 2D ArrayList in Java
The following article provides an outline for 2D ArrayList in Java. In java array list can be two dimensional, three dimensional etc. The basic format of the array list is being one dimensional. Apart from one dimensional all other formats are considered to be the multi-dimensional ways of declaring arrays in java. Based on the number of dimensions expected to be added the number of arrays needs to be added. Moreover, an array list is very close to an array. An array list is a dynamic item. The same applies to a two-dimensional array list. These multidimensional arrays are very similar to the dynamic arrays where the size cannot be predefined.
Syntax:
import java.util.*;
ArrayList<data_type> arrayList = new ArrayList<> ();
ArrayList<data_type> list_name = new ArrayList<>(int capacity);
The above given is the syntax for array list creation in java, the array list needs to be created with the arraylist keyword as the first item. The array list forms the first item and then the data type of the array list needs to be declared. The array list data type needs to be followed by the list name. The name of the list value given here will be the actual list value expected. Next, the array list object needs to be created and this value is created with new as the arraylist.
How 2D ArrayList Works?
Some among the key characteristics of the array list are given below:
- The insertion order can be maintained by java ArrayList corresponding to the value insertion triggered.
- The two dimensional arrays allow duplicates to be stored in it. So the same value can be entered more than once in two dimensional arrays’ point of the case. This is another property which makes array list a close comparison to arrays. Array also has its own indices.
- Synchronization is not performed in this kind of array list items, this is one among the key items which differentiate the two dimensional ArrayList from the vectors, vectors are also elements java which does the same operation as two dimensional and multi-dimensional array lists, the key difference generated between these items in this statement. The capability to be non-synchronized.
- When compared to C++ elements these array lists are very closely related to vectors. The vectors in C++ and the array lists in java are intended to perform the same sought of operation in the background. This is another property which makes array list a close comparison to arrays. the array also has its own indices.
- Random access is a granted item in array lists. This means any specific item in the two dimensional array list can be reached through a pointer or a different reference. This is another property which makes array list a close comparison to arrays. Array also has its own indices. More predominantly the capability to reach each item of the array list without correspondence to the order is a key advantage in multidimensional and two-dimensional array lists. The space for the 0th row can be allocated with the use of a new keyword, this is done in this line. The 0th row also allows the store of 0 value as default. Next, the array list value is replaced with a new value. The replacement involves changing the value from 0 to 13. the value after array list changes is printed onto the console.
- The operations that control factors withinside the ArrayList are gradual as numerous transferring of factors desires to be performed if any detail is to be eliminated from the ArrayList.
- The ArrayList elegance cannot incorporate primitive kinds however best objects. In this case, we commonly name it as ‘ArrayList of objects. So in case you need to save integer kinds of elements, then you need to use the Integer item of the wrapper elegance and now no longer primitive kind int.
A sample diagrammatic representation of how two-dimensional array’s work in java, we can notice from the picture that each column is represented with the row and column level indices values. The first indice represents the row value whereas the second indices represent the column value. This is represented in the format of a[0][0] , a[0][1] etc.
Example of 2D ArrayList in Java
Given below is the example mentioned:
Code:
import java.util.*;
public class Two_Dimensional_ArrayLists{
public static void main(String args[]) {
// The arraylist of 2d format will be declared here
ArrayList<ArrayList<Integer> > array_list = new ArrayList<ArrayList<Integer> >();
// The space for the 0th row can be allocated with the use of new keyword, this is done in this line. The 0th row also allows the store of 0 value as default .
array_list.add(new ArrayList<Integer>());
// next the default value of 1 is changed to 13 here.
array_list.get(1).add(0, 13);
System.out.println("2D ArrayList… :");
System.out.println(array_list);
}
}
Output:
Explanation:
- The example explains the process of creating a 2-dimensional array list and then adding a value to the array list and then the value is attempted to be replaced with a different value. The first key process is to declare the headers for creating the two dimensional array list. In our case ‘import java.util.* ’. Next a class is declared. The declared class has the main function associated with it. The main function has the new array declared. So the declaration step for the array is placed with the main function.
- The array is declared as per the array list values. Next the add function is used for adding the values into the array list. The space for the 0th row can be allocated with the use of a new keyword, this is done in this line. The 0th row also allows the store of 0 value as default. Next the array list value is replaced with a new value. The replacement involves changing the value from 0 to 13. The value after arraylist changes is then printed onto the console.
Conclusion
The article shows the process of creating a two-dimensional array list. The syntax of creating the array list and key characteristics of the array list along with a suitable example is shown in this article.
Recommended Articles
This is a guide to 2D ArrayList in Java. Here we discuss the introduction, how 2D ArrayList works? and example respectively. You may also have a look at the following articles to learn more –