Create Dynamic 2D array in java

Let’s learn to create dynamic 2D array in java.

Create dynamic 2D array in java

Dynamic two dimensional array is one array where user can add and remove rows and columns on need and can have varying number of rows and columns.

To implement dynamic two dimensional array we use a combination of List(can shrink and grow) and int[]. Here’s an example to create dynamic two dimensional array using List.

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

Output:

Element at [0][0]: 4
Element at [1][1]: 5


Also read – preface to java virtual machine and architecture