I want to create a 2D array that each cell is an ArrayList!

If you want to create a 2D array of ArrayList.Then you can do this :

ArrayList[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList(); // add another ArrayList object to [0,0]
table[0][0].add(); // add object to that ArrayList
Answer from AllTooSir on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java list › multi dimensional arraylist in java
Multi Dimensional ArrayList in Java | Baeldung
January 8, 2026 - In this article, we discussed how to create a multidimensional ArrayList in Java. We saw how we can represent a graph using a 2-D ArrayList.
Discussions

How do I create a 2d ArrayList in java?
Fast and easy solution: You can make an arraylist of an object which is of type arraylist More on reddit.com
🌐 r/learnjava
16
26
June 25, 2020
java - Two dimensional array list - Stack Overflow
You can find more information on how to write good answers in the help center. 2021-10-19T10:44:45.18Z+00:00 ... Here's how to make and print a 2D Multi-Dimensional Array using the ArrayList Object. import java.util.ArrayList; public class TwoD_ArrayListExample { static public ArrayList More on stackoverflow.com
🌐 stackoverflow.com
How to create a 2D ArrayList - Processing Forum
An array list can hold any kind of object, including other collections, for example an array list... A 2D array uses the same idea: it is only an array of arrays. ... Okay, how do I declare a multi-dimension arraylist ? More on forum.processing.org
🌐 forum.processing.org
February 20, 2013
Java 2D ArrayList - Free Support Forum - aspose.com
How would one use importArrayList when you have a 2D ArrayList as ArrayList ? More on forum.aspose.com
🌐 forum.aspose.com
1
0
September 5, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › java › multidimensional-collections-in-java
Multidimensional Collections in Java - GeeksforGeeks
July 11, 2025 - Therefore, if we want to use a ... Multidimensional collections in java. ... void add( int index, ArrayList<Object> e): It is used to insert the elements at the specified position in a Collection....
🌐
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
🌐
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

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");

}
🌐
Coderanch
coderanch.com › t › 683236 › java › adding-objects-ArrayList
adding objects to a 2D ArrayList (Java in General forum at Coderanch)
August 10, 2017 - JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility ... I am not sure you have got the concept of the relationship between Lists and Arrays. Because of peculiarities in generics, you cannot create an array of Lists of Strings as you showed (there are some fiddles you can employ depending on how strong your conscience is, however ‍). So you have declared an array of arrays (as Rob told you, it isn't a 2D array), and that is its type.
Find elsewhere
🌐
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 - 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.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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 - import java.util.ArrayList; import java.util.Arrays; public class Main { public static void main(String[] args) { ArrayList<String>[][] arraylist1 = new ArrayList[3][3]; arraylist1[0][0] = new ArrayList<String>(); arraylist1[0][0].add("String One"); arraylist1[0][0].add("String Two"); arraylist1[0][0].add("String Three"); arraylist1[0][1] = new ArrayList<String>(); arraylist1[0][1].add("String One"); arraylist1[0][1].add("String Two"); arraylist1[0][1].add("String Three"); arraylist1[0][2] = new ArrayList<String>(); arraylist1[0][2].add("String One"); arraylist1[0][2].add("String Two"); arrayl
🌐
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 and is supported in Java's collection library. In this article, we have focused on 2D array list in Java along with different methods applicable on it like indexOf.
🌐
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.
🌐
Aspose
forum.aspose.com › aspose.cells product family
Java 2D ArrayList - Free Support Forum - aspose.com
September 5, 2022 - How would one use importArrayList when you have a 2D ArrayList as ArrayList<ArrayList<String>>?
🌐
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.

🌐
javaspring
javaspring.net › blog › how-to-create-an-2d-arraylist-in-java
How to Create a 2D ArrayList in Java: Correct Initialization and Adding Elements Guide — javaspring.net
This is where a **2D ArrayList** comes into play. A 2D ArrayList is essentially an "ArrayList of ArrayLists"—where the outer ArrayList holds multiple inner ArrayLists, each representing a row or a sublist.
🌐
Rose-Hulman Institute of Technology
rose-hulman.edu › class › csse › csse221 › 201410 › Capsules › Summaries › 07ArraysAndArrayListsSec2.pdf pdf
1D and 2D Arrays and ArrayLists Sources
You type out ArrayList<object type here>() and you don’t need to ... Int[][] newArray2D = new int[5][6]; (Creates a 2D Array with 5 rows and 6 columns that holds integers)
🌐
TutorialsPoint
tutorialspoint.com › multidimensional-collections-in-java
Multidimensional Collections in Java
March 19, 2025 - ArrayList<ArrayList<Object>> object_name = new ArrayList<ArrayList<Object>>(); Following are the steps to create a multidimensional collection using nested ArrayList ? ArrayList<ArrayList<Integer>> A list of lists to create a flexible 2D structure.
🌐
Quora
quora.com › How-do-I-convert-a-list-of-list-integers-to-2D-int-array-in-Java
How to convert a list of list integers to 2D int array in Java - Quora
// List<List<Integer>> res = new ArrayList<>(); int[][] ints = res.stream().map(x -> x.stream().mapToInt(Integer::intValue).toArray()).toArray(int[][]::new); Use java stream. First is convert List<Integer> to int[], and then convert to 2D int array. ...
🌐
GitHub
gist.github.com › jaksonkallio › 79b823b98febae2f97e1aeb5f635fe3a
Java 2D ArrayList · GitHub
Save jaksonkallio/79b823b98febae2f97e1aeb5f635fe3a to your computer and use it in GitHub Desktop. Download ZIP · Java 2D ArrayList · Raw · ArrayListImplementaton.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Easyprogramming
easyprogramming.in › Tutorial › 19-2d-arrays-and-arraylists › understanding-2d-arrays-and-arraylists
Easy Programming - Understanding 2D Arrays and ArrayLists
Here number 8 in bold can be fetched ... ... To declare a 2D ArrayList, you specify the type of elements it will hold and use two pairs of angle brackets to indicate a 2D structure:...