You would use

List<List<String>> listOfLists = new ArrayList<List<String>>();

And then when you needed to add a new "row", you'd add the list:

listOfLists.add(new ArrayList<String>());

I've used this mostly when I wanted to hold references to several lists of Point in a GUI so I could draw multiple curves. It works well.

For example:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class DrawStuff extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private static final Color POINTS_COLOR = Color.red;
   private static final Color CURRENT_POINTS_COLOR = Color.blue;
   private static final Stroke STROKE = new BasicStroke(4f);
   private List<List<Point>> pointsList = new ArrayList<List<Point>>();
   private List<Point> currentPointList = null;

   public DrawStuff() {
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setStroke(STROKE);
      g.setColor(POINTS_COLOR);
      for (List<Point> pointList : pointsList) {
         if (pointList.size() > 1) {
            Point p1 = pointList.get(0);
            for (int i = 1; i < pointList.size(); i++) {
               Point p2 = pointList.get(i);
               int x1 = p1.x;
               int y1 = p1.y;
               int x2 = p2.x;
               int y2 = p2.y;
               g.drawLine(x1, y1, x2, y2);
               p1 = p2;
            }
         }
      }
      g.setColor(CURRENT_POINTS_COLOR);
      if (currentPointList != null && currentPointList.size() > 1) {
         Point p1 = currentPointList.get(0);
         for (int i = 1; i < currentPointList.size(); i++) {
            Point p2 = currentPointList.get(i);
            int x1 = p1.x;
            int y1 = p1.y;
            int x2 = p2.x;
            int y2 = p2.y;
            g.drawLine(x1, y1, x2, y2);
            p1 = p2;
         }
      }
   }

   private class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent mEvt) {
         currentPointList = new ArrayList<Point>();
         currentPointList.add(mEvt.getPoint());
         repaint();
      }

      @Override
      public void mouseDragged(MouseEvent mEvt) {
         currentPointList.add(mEvt.getPoint());
         repaint();
      }

      @Override
      public void mouseReleased(MouseEvent mEvt) {
         currentPointList.add(mEvt.getPoint());
         pointsList.add(currentPointList);
         currentPointList = null;
         repaint();
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("DrawStuff");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new DrawStuff());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Answer from Hovercraft Full Of Eels on Stack Overflow
Top answer
1 of 10
92

You would use

List<List<String>> listOfLists = new ArrayList<List<String>>();

And then when you needed to add a new "row", you'd add the list:

listOfLists.add(new ArrayList<String>());

I've used this mostly when I wanted to hold references to several lists of Point in a GUI so I could draw multiple curves. It works well.

For example:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class DrawStuff extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private static final Color POINTS_COLOR = Color.red;
   private static final Color CURRENT_POINTS_COLOR = Color.blue;
   private static final Stroke STROKE = new BasicStroke(4f);
   private List<List<Point>> pointsList = new ArrayList<List<Point>>();
   private List<Point> currentPointList = null;

   public DrawStuff() {
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setStroke(STROKE);
      g.setColor(POINTS_COLOR);
      for (List<Point> pointList : pointsList) {
         if (pointList.size() > 1) {
            Point p1 = pointList.get(0);
            for (int i = 1; i < pointList.size(); i++) {
               Point p2 = pointList.get(i);
               int x1 = p1.x;
               int y1 = p1.y;
               int x2 = p2.x;
               int y2 = p2.y;
               g.drawLine(x1, y1, x2, y2);
               p1 = p2;
            }
         }
      }
      g.setColor(CURRENT_POINTS_COLOR);
      if (currentPointList != null && currentPointList.size() > 1) {
         Point p1 = currentPointList.get(0);
         for (int i = 1; i < currentPointList.size(); i++) {
            Point p2 = currentPointList.get(i);
            int x1 = p1.x;
            int y1 = p1.y;
            int x2 = p2.x;
            int y2 = p2.y;
            g.drawLine(x1, y1, x2, y2);
            p1 = p2;
         }
      }
   }

   private class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent mEvt) {
         currentPointList = new ArrayList<Point>();
         currentPointList.add(mEvt.getPoint());
         repaint();
      }

      @Override
      public void mouseDragged(MouseEvent mEvt) {
         currentPointList.add(mEvt.getPoint());
         repaint();
      }

      @Override
      public void mouseReleased(MouseEvent mEvt) {
         currentPointList.add(mEvt.getPoint());
         pointsList.add(currentPointList);
         currentPointList = null;
         repaint();
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("DrawStuff");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new DrawStuff());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
2 of 10
22

You can create a list,

ArrayList<String[]> outerArr = new ArrayList<String[]>(); 

and add other lists to it like so:

String[] myString1= {"hey","hey","hey","hey"};  
outerArr .add(myString1);
String[] myString2= {"you","you","you","you"};
outerArr .add(myString2);

Now you can use the double loop below to show everything inside all lists

for(int i=0;i<outerArr.size();i++){

   String[] myString= new String[4]; 
   myString=outerArr.get(i);
   for(int j=0;j<myString.length;j++){
      System.out.print(myString[j]); 
   }
   System.out.print("\n");

}
🌐
Reddit
reddit.com › r/learnjava › how do i create a 2d arraylist in java?
r/learnjava on Reddit: How do I create a 2d ArrayList in java?
June 25, 2020 -

I want to implement breadth first search in java. However I am stuck at making a adjacency list of the graph. The method on GeeksforGeeks declares the list as LinkedList<Integer> adj[]; Well this is actually an array of List and not dynamic too. Like you'll have to change the array size when you want to add some extra node. So, I tried List<List<Integer>> adj = new ArrayList<List<Integer>>(); and then made the inner ArrayList in a for loop as adj.add(new ArrayList<Integer>);. I am trying from so long still getting this error 'List cannot be resolved to a type'. I searched on Stack overflow and there too they have the same declaration as mine so I don't really know what I am doing wrong. Please help me.

Edit: I am using Java 11

🌐
Baeldung
baeldung.com › home › java › java list › multi dimensional arraylist in java
Multi Dimensional ArrayList in Java | Baeldung
January 8, 2026 - We can represent the edges in a 2-D ArrayList by creating and populating an ArrayList of ArrayLists.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
Python creates only one inner list and one 0 object, not separate copies. This shared reference behavior is known as shallow copying (aliasing). If we assign the 0th index to another integer say 1, then a new integer object is created with the value of 1 and then the 0th index now points to this new int object as shown below · Similarly, when we create a 2d array as "arr = [[0]*cols]*rows" we are essentially extending the above analogy.
Published   December 20, 2025
🌐
Scaler
scaler.com › home › topics › how to create 2d arraylist in java?
How to Create 2D ArrayList in Java? - Scaler Topics
April 20, 2024 - We provide you with a 2d ArrayList java example to serve as an example. ... Here we've created an ArrayList named as arraylist which stores 3 characters C, A, and T in the form of a list, we've used Arrays.aslist() for returning characters as a list like
🌐
GeeksforGeeks
geeksforgeeks.org › java › multidimensional-collections-in-java
Multidimensional Collections in Java - GeeksforGeeks
July 11, 2025 - // Java Program to Illustrate Multidimensional ArrayList // Importing required classes import java.util.*; // Main class // MultidimensionalArrayList class GFG { // Method 1 // To create and return 2D ArrayList static List create2DArrayList() { // Creating a 2D ArrayList of Integer type ArrayList<ArrayList<Integer> > x = new ArrayList<ArrayList<Integer> >(); // One space allocated for R0 x.add(new ArrayList<Integer>()); // Adding 3 to R0 created above x(R0, C0) x.get(0).add(0, 3); // Creating R1 and adding values // Note: Another way for adding values in 2D // collections x.add( new ArrayList<
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › 2d arraylist in java
2D ArrayList in Java | How 2D ArrayList Works | Examples
April 18, 2023 - 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.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Coderanch
coderanch.com › t › 683236 › java › adding-objects-ArrayList
adding objects to a 2D ArrayList (Java in General forum at Coderanch)
August 10, 2017 - Pedantic mode: there is no such thing as a 2D array or list. All you have is an array of arrays or a list of lists. Your array has the wrong inner type. Your List<List<String>> is a List of Lists of String.
🌐
Delft Stack
delftstack.com › home › howto › java › create a 2d arraylist in java
How to Create a 2D ArrayList in Java | Delft Stack
February 2, 2024 - To insert an innerArraylist function inside outerArrayList1, we can initialize the 2D ArrayList Java object to outerArrayList1. The next and the last step is adding our data to the innerArraylist function and then adding it to the outerArrayList command. Note that we can add more than one ArrayLists into the outerArrayList command. import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { ArrayList<String> innerArraylist; innerArraylist = new ArrayList<String>(); List<ArrayList<String>> outerArrayList = new ArrayList<>(); innerArraylist.add("String One"); innerArraylist.add("String Two"); innerArraylist.add("String Three"); outerArrayList.add(innerArraylist); System.out.println(outerArrayList.toString()); } }
🌐
Java2Blog
java2blog.com › home › core java › java collections › 2d arraylist java example
2d Arraylist java example - Java2Blog
January 10, 2021 - Let’s create a program to implement 2d Arraylist java. ... 2nd element in list3 : List3_Str2 3nd element in list1 : List1_Str3 1st element in list2 : List2_Str1
🌐
Processing Forum
forum.processing.org › topic › how-to-create-a-2d-arraylist
How to create a 2D ArrayList - Processing Forum
February 20, 2013 - The code below creates a 2D ArrayList of String objects, simple replace String with the name of your class, replace the new String() constructor in line 11 with one for your class and you (if you want) override the toString method to create your own custom output string for your class. ... The way you did, you don't declare anything special: you just add() an array list to this array list.
🌐
Snakify
snakify.org › two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
In real-world Often tasks have to store rectangular data table. [say more on this!] Such tables are called matrices or two-dimensional arrays. In Python any table can be represented as a list of lists (a list, where each element is in turn a list).
🌐
OpenGenus
iq.opengenus.org › 2d-array-list-java
2D Array List in Java
May 9, 2020 - An Array List is a dynamic version of array. It is similar to Dynamic Array class where we do not need to predefine the size. The size of array list grows automatically as we keep on adding elements. In this article, we will focus on 2D array list in Java.
🌐
Rose-Hulman Institute of Technology
rose-hulman.edu › class › csse › csse221 › 201410 › Capsules › Summaries › 07ArraysAndArrayListsSec2.pdf pdf
1D and 2D Arrays and ArrayLists Sources
An ArrayList is just like a 1D Array except it’s length is unbounded and you can add as many elements as you need. In order to create a 1D or 2D Array you need to specify the type of object that you are going to be storing in the
🌐
Processing
processing.org › tutorials › 2darray
Two-Dimensional Arrays / Processing.org
A two-dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays). Think of your dinner. You could have a one-dimensional list of everything you eat:
🌐
LeetCode
leetcode.com › discuss › general-discussion › 910108 › java-converting-2d-list-into-array
[JAVA] Converting 2D List into Array. - Discuss - LeetCode
Another way could be as below, which is not often useful for int arrays, but for strings Integer[] arr = list.toArray(new Integer[list.size()]); This will not work if we use primitive data type like int. Of course above might help for converting list of Strings to array of strings, Everything becomes interesting when it is a 2D array.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java arraylist: how to declare, create, initialize arraylist
Java ArrayList: How to Declare, Create, Initialize ArrayList
April 7, 2020 - Note that you can increase the nested levels of ArrayList to define multi-dimensional ArrayLists. For example, 3D ArrayList will have 2D ArrayLists as its elements and so on.
🌐
Coderanch
coderanch.com › t › 754467 › java › array-list-strings-array-strings
From array list of strings to a 2d array of strings (Beginning Java forum at Coderanch)
September 20, 2022 - I'm having trouble converting it to a 2d array. Please explain. Give a simple list and the desire 2D array from that list. Can you post the current results from the code so we can see what it is doing? Use the Arrays.deepToString method to format the array for printing.
🌐
Reddit
reddit.com › r/java › two dimensional arraylist, need help with constructor
r/java on Reddit: Two dimensional ArrayList, need help with constructor
February 17, 2012 -

Hello there fine java community. So my project is to make the game Memory in java, now im not going to bore you with all the details and all my classes. But i have a problem right at the very start, i need a two dimensional array, and I will be using an ArrayList as it needs to be dynamic. The size can change based on how big the user wants it. Now i was thinking something like this:

List<List<E>> array;

To declarate a twodimensional arraylist called "array", and initialize it in the constructor.

This is my question basically, how would you do it?

Edit: Typo.

Edit: For clarification, the array will hold objects of a certain type. Meaning if it was an generic array the:

array[x][y];

Would return the object that lays on the spot x, y.