How to create a dynamic 2D array in Java?

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 have any number of rows or columns.


import java.util.ArrayList;
import java.util.List;
public class Tester {
   public static void main(String[] args) {
      List<int[]> rows = new ArrayList<>();
      rows.add(new int[]{1,2,3});
      rows.add(new int[]{1,2});
      rows.add(new int[]{1});
      //get element at row : 0, column : 0
      System.out.println("[0][0] : " + rows.get(0)[0]);
      //get element at row : 1, column : 1
     System.out.println("[1][1] : " + rows.get(1)[1]);


[0][0] : 1
[1][1] : 2

Updated on: 24-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started