1. int[][] board = new int[][] {{1,0,0},{0,1,0},{1,2,1}}

please explain how we have used index here....like row and column....i know about this we have an array, a, with two rows and four columns.

int[][] a = new int[2][4];

// Two rows and four columns.

but what about the 1. as in that we don't have actual index provided....what will be the index in that.....

7 Years
Discussion Span
Last Post by JamesCherrill

There's no such thing in Java as a 2D array. Java has 1D arrays, but each element of that array can be another array (etc). It's a subtle distinction, but important.
eg int[][] a = {{1},{2,3},{4,5,6,7}} defines an array of arrays of ints, but every array is a different size.
To index the elements of an array inside an array you just index the outermost array (which returns an inner array) then index that array (etc).
so, with the example above a[1] is the array {2,3} (you can say int[] b = a[1]; that's perfectly OK) a[1][1] is 3 a[2][3] is 7 a[1][3] is an error because the array returned by a[1] has only 2 elements int[][] a = new int[2][4]; creates an array with two elements, each of those elements is an array of 4 ints.

Edited by JamesCherrill: n/a


int[][] board = new int[][] {{1,0,0},{0,1,0},{1,2,1}} The array literal {{1,0,0},{0,1,0},{1,2,1}} defines an array of 3 elements, each of which is an array of 3 ints, so it is the same size as new int[3][3]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.