//Array2DTest.java // demo of Java two-dimensional arrays import javax.swing.*; public class Array2DTest { public static void main(String[] args) { //2D array of ints: 10 rows, 20 columns, like a table. //10*20=200 elements. //Each row has 20 elements. Each row is itself an array. //Syntax is two pairs of brackets. int [][] my2DArray = new int[10][20]; //nested for loops to access all elements. //outer loop loops over rows, //inner loop loops over columns (elements of that row). for (int i=0; i<10; i++) { //i is conventional for row index for (int j=0; j<20; j++) { //j is conventional for column index //dummy value that is offset from base of the array for illustration purposes my2DArray[i][j] = i*20 + j; //indexes are [row][column] System.out.print(" "+my2DArray[i][j]); } System.out.println(); //end of the row } //row and column sizes can be variables: size determined at runtime. String input = JOptionPane.showInputDialog( "Enter number of rows" ); int rows = Integer.parseInt( input ); input = JOptionPane.showInputDialog( "Enter number of columns" ); int cols = Integer.parseInt( input ); System.out.println(); //re-instantiate the 2D array my2DArray = new int[rows][cols]; for (int i=0; i