Hi All, Consider the following code <code>class Test { public static void main(String[] args) { int i = 4; int ia[][][] = new int[i][i = 3][i]; System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length); } } </code> the answer is 4,3,3 can somebody explained how the compiler evaluate this result ------------------ coffee drinker
Originally posted by amit mawkin: Hi All, Consider the following code <code>class Test { public static void main(String[] args) { int i = 4; int ia[][][] = new int[i][i = 3][i]; System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length); } } </code> the answer is 4,3,3 can somebody explained how the compiler evaluate this result
If i understand correctly, its evaluated from left to right Initial value of i will be 4 and once i becomes 3 subsequent values of i gets changed as well. In a multi-dimensional matrix, int [3][] //Second value is not mandatory and the length is 3 Same applies here int a[4][3][3]//Length is 4 Please correct me if i am wrong Thankx Ragu
amit mawkin
Ranch Hand
Joined: Oct 31, 2001
Posts: 73
posted
0
Hi Ragu, Thanx for your reply but still not clear to me its ok that while saying <code> i=4; int x[][][]=new int[i][i=3][i]; </code> that third bracket will have a value of 3 fine but look at its print statement <code> System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length); </code> generally speaking the length of an array represents no of elements in it . so if i say <code> int a[][]=new int[4][3]; </code> it means array will hold 4*3 12 elements also in java Correct me if I am wrong multidimensional arrays are implemented as arrays of arrays. so when we say ia.length why we get 4 instead of 4*3*3 But as we are reffering to whole arrays not a a subarray inside it. I can very well get your point that in a 2 dimensional arrays the second part could be left intentionally blank . I am weak in multidimensional arrays,so if this sort of question comes in my scjp i am definitely goona have to use hit and try method.
[This message has been edited by amit mawkin (edited November 19, 2001).]
Ragu Sivaraman
Ranch Hand
Joined: Jul 20, 2001
Posts: 464
posted
0
Three-dimensional arrays are treated like 2 dimensional. For example, a three-dimensional array of ints could be created with the declaration statement "int[][][] B = new int[7][5][11];". It's possible to visualize the value of B as a solid 7-by-5-by-11 block of cells. Each cell holds an int and represents one position in the three-dimensional array. Individual positions in the array can be referred with variable names of the form B[i][j][k]. Ragu
amit mawkin
Ranch Hand
Joined: Oct 31, 2001
Posts: 73
posted
0
so ragu what are the toatl no ints that can be stored in such an array is it 7*5*11 ------------------ coffee drinker