2d Arraylist Java Example Tutorial With Code Snippets
Robert
Jul 13, 2026
A 2D ArrayList in Java is an ArrayList of ArrayLists, allowing us to store and manipulate 2D data structures, such as matrices and tables, with relative ease. By combining the power of ArrayLists with Java's Object-Oriented Programming (OOP) principles, we can create compact, efficient code for a variety of tasks.
What are Arrays in Java?
Understanding how to work with 2D ArrayLists is crucial for many applications, from scientific computing to data analysis and machine learning. In this guide, we will walk you through creating, accessing, and manipulating 2D ArrayLists in Java, complete with illustrative examples.
The first step towards working with 2D ArrayLists is knowing how to initialize them. You can start with an empty 2D ArrayList or initialize it with some predefined values.
How to convert an ArrayList to Array in Java? Example
Here's how to create a 2D ArrayList of integers:
```java
ArrayList> twoDArrayList = new ArrayList<>();
```
Initializing with predefined values
Multidimensional Array in Java (2D Array) - Scientech Easy
To initialize a 2D ArrayList with predefined values, you first need to create an ArrayList for each row and then add these to the main 2D ArrayList. Here's an example:
```java
ArrayList> twoDArrayList = new ArrayList<>();
ArrayList row1 = new ArrayList<>();
row1.add(1);
row1.add(2);
row1.add(3);
ArrayList row2 = new ArrayList<>();
row2.add(4);
row2.add(5);
row2.add(6);
twoDArrayList.add(row1);
twoDArrayList.add(row2);
```
Accessing elements in a 2D ArrayList
Accessing elements in a 2D ArrayList is done by specifying the row number followed by the column number, starting from 0.
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Here's how to access the element at the second row and third column:
```java
int value = twoDArrayList.get(1).get(2); // value = 6
```
Manipulating a 2D ArrayList
Once you have initialized your 2D ArrayList, you can add, remove, and modify its contents as needed.
16 Examples of ArrayList in Java - Tutorial
Adding a new row
To append a new row to the end of the 2D ArrayList, follow these steps:
Sorting ArrayList in Java using Collections.sort() MethodJava Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |JavaScript Arrays: Master Data Storage & Manipulation 📊an image of a computer screen with the words and numbers on it, including two arrows30 Days of Java – Day 22 | ArrayList vs LinkedList Explained for BeginnersArrays in Java | Array Declaration - Scientech Easy2D Array in JavaC Programming - 2D ArraysArrays unidimensionales en Javaa screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description descriptionHow to declare and Initialize two dimensional Array in Java with Example30 Days of Java – Day 7 | Java Arrays Explained for Beginnersan open book with instructions on how to use the array array in windows and linux30 Days of Java – Day 21 | Java ArrayList Explained for BeginnersDifference Between Array and ArrayList in Java - TechVidvanan image of a computer screen showing the instructions for using one dimensional arrays to solve problemsArrays in Java Explained | Java Programming Basics for BeginnersJava Cheatsheeta poster with different types of writing and numbers on it, including the words'jwa beginner notes 'Java List Interface | Methods, Example - Scientech Easy
Removing a row is done by specifying the row index, using the `remove()` method:
```java
twoDArrayList.remove(1); // Removes the second row
```
Remember that array indices start from 0, so the first row is at index 0.
Now that you've mastered the basics of 2D ArrayLists in Java, you can venture into more complex tasks, such as sorting, searching, or even creating custom data structures tailored to your needs. The possibilities are endless!