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?
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.

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

Creating a 2D ArrayList

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
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
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
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
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() Method
Sorting ArrayList in Java using Collections.sort() Method
Java Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |
Java Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |
JavaScript Arrays: Master Data Storage & Manipulation 📊
JavaScript Arrays: Master Data Storage & Manipulation 📊
an image of a computer screen with the words and numbers on it, including two arrows
an image of a computer screen with the words and numbers on it, including two arrows
30 Days of Java – Day 22 | ArrayList vs LinkedList Explained for Beginners
30 Days of Java – Day 22 | ArrayList vs LinkedList Explained for Beginners
Arrays in Java | Array Declaration - Scientech Easy
Arrays in Java | Array Declaration - Scientech Easy
2D Array in Java
2D Array in Java
C Programming - 2D Arrays
C Programming - 2D Arrays
Arrays unidimensionales en Java
Arrays unidimensionales en Java
a 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 description
a 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 description
How to declare and Initialize two dimensional Array in Java with Example
How to declare and Initialize two dimensional Array in Java with Example
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
an open book with instructions on how to use the array array in windows and linux
an open book with instructions on how to use the array array in windows and linux
30 Days of Java – Day 21 | Java ArrayList Explained for Beginners
30 Days of Java – Day 21 | Java ArrayList Explained for Beginners
Difference Between Array and ArrayList in Java - TechVidvan
Difference Between Array and ArrayList in Java - TechVidvan
an image of a computer screen showing the instructions for using one dimensional arrays to solve problems
an image of a computer screen showing the instructions for using one dimensional arrays to solve problems
Arrays in Java Explained | Java Programming Basics for Beginners
Arrays in Java Explained | Java Programming Basics for Beginners
Java Cheatsheet
Java Cheatsheet
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
Java List Interface | Methods, Example - Scientech Easy
Java List Interface | Methods, Example - Scientech Easy

```java ArrayList newRow = new ArrayList<>(); newRow.add(7); newRow.add(8); newRow.add(9); twoDArrayList.add(newRow); ```

Removing a row

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!

Happy coding, and until next time!