Declaration
int[][] multiples = new int[4][5]; // 2D integer array
with 4 rows and 5 columns
String[][] cities = new String[2][3]; // 2D String
array with 2 rows and 3 columns
When you declare 2 dimensional array must remember to specify first dimension
Example
int[][] wrong = new int[][]; // error at compile time
int[][] right = new int[2][]; // not show error
Initialize
boolean[][] booleans = new boolean[3][3];
String[][] strings= new string[3][3];
byte[][] bytes = new byte[2][2];
char[][] chars = new char[1][1];
char[][] chars = new char[1][1];
short[][] shorts = new short[2][2];
int[][] ints = new int[2][2];
Example
/*two dimensional array example in java */
//programming789.blogspot.com/
import java.util.Arrays;
class ArrayExample
{
public static void main(String args[])
{
String[][] names = { {"John", "Cena"}, {"Jack", "Taylor"}, {"James", "Brown"}, };
System.out.println(names[0][0]+" " +names[0][1]);
int[][] no={{1,2},{4,1},{6,2},};
System.out.println(no[0][0]+" " +no[0][1]);
}
}
● Save the file as ArrayExample.java.
Output:
John Cena
1 2