Since the number of columns is a constant, you can just have an List of int[].

    import java.util.*;
    //...

    List<int[]> rowList = new ArrayList<int[]>();

    rowList.add(new int[] { 1, 2, 3 });
    rowList.add(new int[] { 4, 5, 6 });
    rowList.add(new int[] { 7, 8 });

    for (int[] row : rowList) {
        System.out.println("Row = " + Arrays.toString(row));
    } // prints:
      // Row = [1, 2, 3]
      // Row = [4, 5, 6]
      // Row = [7, 8]

    System.out.println(rowList.get(1)[1]); // prints "5"

Since it's backed by a List, the number of rows can grow and shrink dynamically. Each row is backed by an int[], which is static, but you said that the number of columns is fixed, so this is not a problem.

Answer from polygenelubricants on Stack Overflow
Top answer
1 of 9
46

Since the number of columns is a constant, you can just have an List of int[].

    import java.util.*;
    //...

    List<int[]> rowList = new ArrayList<int[]>();

    rowList.add(new int[] { 1, 2, 3 });
    rowList.add(new int[] { 4, 5, 6 });
    rowList.add(new int[] { 7, 8 });

    for (int[] row : rowList) {
        System.out.println("Row = " + Arrays.toString(row));
    } // prints:
      // Row = [1, 2, 3]
      // Row = [4, 5, 6]
      // Row = [7, 8]

    System.out.println(rowList.get(1)[1]); // prints "5"

Since it's backed by a List, the number of rows can grow and shrink dynamically. Each row is backed by an int[], which is static, but you said that the number of columns is fixed, so this is not a problem.

2 of 9
20

There are no multi-dimensional arrays in Java, there are, however, arrays of arrays.

Just make an array of however large you want, then for each element make another array however large you want that one to be.

int array[][];

array = new int[10][];

array[0] = new int[9];
array[1] = new int[8];
array[2] = new int[7];
array[3] = new int[6];
array[4] = new int[5];
array[5] = new int[4];
array[6] = new int[3];
array[7] = new int[2];
array[8] = new int[1];
array[9] = new int[0];

Alternatively:

List<Integer>[] array;

array = new List<Integer>[10];

// of you can do "new ArrayList<Integer>(the desired size);" for all of the following
array[0] = new ArrayList<Integer>();
array[1] = new ArrayList<Integer>();
array[2] = new ArrayList<Integer>();
array[3] = new ArrayList<Integer>();
array[4] = new ArrayList<Integer>();
array[5] = new ArrayList<Integer>();
array[6] = new ArrayList<Integer>();
array[7] = new ArrayList<Integer>();
array[8] = new ArrayList<Integer>();
array[9] = new ArrayList<Integer>();
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-create-a-dynamic-2D-array-in-Java
How to create a dynamic 2D array in Java?
February 24, 2020 - If you wish to create a dynamic 2d array in Java without using List. And only create a dynamic 2d array in Java with normal array then click the below link You can achieve the same using List. See the below program. You can
Discussions

java - Dynamic two dimensional array - Stack Overflow
How can create a dynamic two dimensional array in Java, and how can I get and set its elements and go through all elements? I saw this post. But in the post one of the dimensions (number of rows) is More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to dynamically increase size of a 2D array - Stack Overflow
I already know how to make a fixed array, if I know how many elements I have. For instance, for 7 elements I do something like int array[2][4]. But what if I have 0 elements at start(which means ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
list - How to create Dynamic Two Dimensional Array [JAVA] - Stack Overflow
How can I make the column of teh array dynamic so that no matter how large the value of lastValue increases it runs. ... An array of ArrayList... but since Java doesn't allow array of generic (you can create non-generic ArrayList[], but there will be warning about type safety), so ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 8, 2013
2D dynamic array using ArrayList in Java - Stack Overflow
I need to implement a 2D dynamic array. The number of rows is fixed, say n. But the number of columns for each row is not fixed and equivalent. For instance, the first row has 3 elements and the se... More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 8, 2011
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ how to create a dynamic 2d array in java
How to create dynamic 2d array in Java with example - CodeSpeedy
November 1, 2018 - A 2d array in Java is an array which can hold the same type of data with a given single name as a concept of row and column cell. Letโ€™s understand this with an easy example. ... Here we got a 2d array which can hold up to 12 elements in it. Because we have declared the array size with 3 rows and 4 columns. ... Where m is the number of rows and n is the number of columns. int[][] my_array = { {5, 12, 73}, {78, 45, 89, 11}, {74}, };
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ multidimensional-arrays-in-java
Java Multi-Dimensional Arrays - GeeksforGeeks
... A 2-D array can be seen as ... 3 rows and 3 columns is shown below: ... Example 1: We can add the values directly to the array while declaring the array....
Published ย  March 14, 2026
Top answer
1 of 6
1

Note that new int[2][4] is an array of int[]; the int[] arrays in the int[][] array are all initially the same length, but there's no requirement that they remain the same length. Any element of the int[][] array can be reassigned an int[] with a different length without affecting the other elements at all. The concept of "rows" and "columns" is a higher-level idea that is not supported by Java arrays.

Using an ArrayList as other answers suggest isn't going to change this. Furthermore, Depending on how you use ArrayList, you may end up with considerable overhead due to autoboxing of int values as Integer objects.

If you want to preserve the rectangular shape of your data, I suggest that you define a Matrix class that keeps all the dimensions consistent. (Or, perhaps better, linearizes the two-dimensional array into a one-dimensional array and does the appropriate subscripting calculations using internally stored row and column sizes. Or, perhaps best, use a well-written matrix library such as JAMA or a primitive collections library like Trove.)

EDIT Here's the start of a simple matrix class that uses a linear storage scheme internally and allows matrix resizing. The data are stored in row-major order and indexing is based at 0.

public class IntMatrix {
    private int rows;
    private int cols;
    private int[] data;

    /**
     * Allocate a matrix with the indicated initial dimensions.
     * @param cols The column (horizontal or x) dimension for the matrix
     * @param rows The row (vertical or y) dimension for the matrix
     */
    public IntMatrix(int cols, int rows) {
        this.rows = rows;
        this.cols = cols;
        data = new int[cols * rows];
    }

    /**
     * Calculates the index of the indicated row and column for
     * a matrix with the indicated width. This uses row-major ordering
     * of the matrix elements.
     * <p>
     * Note that this is a static method so that it can be used independent
     * of any particular data instance.
     * @param col The column index of the desired element
     * @param row The row index of the desired element
     * @param width The width of the matrix
     */
    private static int getIndex(int col, int row, int width) {
        return row * width + col;
    }

    public int get(int col, int row) {
        return data[getIndex(col, row, cols)];
    }

    public void set(int col, int row, int value) {
        data[getIndex(col, row, cols)] = value;
    }

    /**
     * Resizes the matrix. The values in the current matrix are placed
     * at the top-left corner of the new matrix. In each dimension, if
     * the new size is smaller than the current size, the data are
     * truncated; if the new size is larger, the remainder of the values
     * are set to 0.
     * @param cols The new column (horizontal) dimension for the matrix
     * @param rows The new row (vertical) dimension for the matrix
     */
    public void resize(int cols, int rows) {
        int [] newData = new int[cols * rows];
        int colsToCopy = Math.min(cols, this.cols);
        int rowsToCopy = Math.min(rows, this.rows);
        for (int i = 0; i < rowsToCopy; ++i) {
            int oldRowStart = getIndex(0, i, this.cols);
            int newRowStart = getIndex(0, i, cols);
            System.arraycopy(data, oldRowStart, newData, newRowStart,
                colsToCopy
            );
        }
        data = newData;
    }

    . . .
}
2 of 6
1

ArrayList documentation, and Examples. Enjoy!

Specifically:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); // Add 1 to the list.
Find elsewhere
๐ŸŒ
Java67
java67.com โ€บ 2014 โ€บ 10 โ€บ how-to-create-and-initialize-two-dimensional-array-java-example.html
How to declare and Initialize two dimensional Array in Java with Example | Java67
If you know how to create a one-dimensional array and the fact that multi-dimensional arrays are just an array of the array in Java, then creating a 2-dimensional array is very easy. Instead of one bracket, you will use two e.g. int[][] is a two-dimensional integer array. You can define a 2D array in Java as follows :
๐ŸŒ
Learningc
learningc.org โ€บ chapters โ€บ chapter09-multi-dimensional-arrays โ€บ 2d-dynamic-memory-alloc
9.3. Dynamic Memory Allocation of 2D Arrays โ€” Snefru: Learning Programming with C
Second, make each of these pointers in the array point to dynamically allocated 1D array corresponding to the row. Third, we can access the elements as any 2D array.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-declare-java-array-with-array-size-dynamically
How to declare Java array with array size dynamically?
To declare array size dynamically read the required integer value from the user using Scanner class and create an array using the given value: import java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray { public static void main(String args[]) { System.out.println("Enter ...
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ blog โ€บ software development โ€บ creating a dynamic array in java
Creating a Dynamic Array in Java
June 12, 2025 - Step 4: While an individual re-allocation is O(n) (linear time complexity), the amortized cost for a sequence of additions averages O(1). This makes the dynamic ยท array in Java highly efficient for most growth patterns.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ memory-allocation-and-storage-for-java-arrays-c68b64ba08bb
Memory Allocation and Storage for Java Arrays | Medium
March 10, 2025 - Learn how Java allocates arrays in heap memory, how indexing works, and how multi-dimensional arrays are structured for efficient storage and access.
๐ŸŒ
GameDev.net
gamedev.net โ€บ forums โ€บ topic โ€บ 223632-how-to-dynamically-allocate-a-2d-array
how to dynamically allocate a 2d array? - For Beginners - GameDev.net
May 8, 2004 - quote: Original post by jimywang if you know the KISS theory(keep it simple stupid),you should alway use the simplest and most up to the tast method to solve the problem.in this case,2d array for map data,there is no need to keep thing in order and no chance to change the map in the run-time.Vector apparently is not the best solution for such a problem.just go for a 2D array mate,it is much better solution. KISS is exactly what we''re talking about. Using a premade multi_array is a lot simpler than managing your own memory for a dynamic multidimensional array, not to mention safer as well, and is even more efficient than using a dynamically allocated array of pointers to the first element of arrays.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 02 โ€บ 6-example-to-declare-two-dimensional-array-in-java.html
6 ways to declare and initialize a two-dimensional (2D) String and Integer Array in Java - Example Tutorial
June 28, 2025 - We have actually declared int[] only Another thing to remember about this code is that if multiple variables are declared in the same line they would be the type of int[] which is one dimensional, not two dimensional like in the following example prices is a 2D array but abc is just a one-dimensional int array. int[] prices[], abc; Again, this is a tricky array concept in Java and that's why you will often find questions on this topic on various Java certifications.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ 2d-array-in-java-two-dimensional-and-nested-arrays
2D Array in Java โ€“ Two-Dimensional and Nested Arrays
August 10, 2022 - A multidimensional array is simply an array of arrays. You can look it as a single container that stores multiple containers. In this article, we'll talk two dimensional arrays in Java. You'll see the syntax for creating one, and how to add and acce...
๐ŸŒ
Ruby-Doc.org
ruby-doc.org โ€บ home โ€บ two dimensional array in java โ€“ the ultimate guide with examples
Two Dimensional Array in Java - The Ultimate Guide with Examples - Ruby-Doc.org
August 5, 2025 - Two dimensional arrays in Java are a powerful and flexible way to represent tabular data. Whether for mathematical operations, data storage, or grid-based applications, mastering 2D arrays is essential for Java programmers. Declared with datatype[][] arrayName. Initialized either statically or dynamically.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays_multi.asp
Java Multi-Dimensional Arrays
Arrays Loop Through an Array Real-Life Examples Multidimensional Arrays Code Challenge ยท Java Methods Java Method Challenge Java Method Parameters
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ learn-java โ€บ modules โ€บ java-two-dimensional-arrays โ€บ cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
//Given a 2d array called `arr` which stores `int` values ... In Java, initializer lists can be used to quickly give initial values to 2D arrays.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ 2d-array-java
2D Array in Java: Configuring Two-Dimensional Arrays
February 27, 2024 - The advantage of this approach is that you can add or remove rows and columns dynamically, which is not possible with a 2D array. However, the downside is that ArrayLists have more overhead than arrays, as they are objects and have additional methods and attributes. The Arrays class in Java provides static methods for manipulating arrays.