-1

I'm using Eclipse Luna and i think it's kind of buggy because when i write:

int[][][] a = new int[2][3][4];
System.out.println(a.length+" "+a[0].length+" "+a[1].length);

Eclipse shows me:

2 3 3

but if i use:

int[][][] a = new int[2][3][4];
System.out.println(a.length+" "+a[0].length+" "+a[0][0].length);

Eclipse shows me:

2 3 4

I don't understand why but because of this reason my whole project doesn't work anymore...

6
  • What do you expect for the lengths of a[0] and a[1]? Should they be different? Commented Oct 4, 2014 at 19:04
  • i used goodle to find out how to find the lengths of multidimensional arrays and they've shown me these technics: a[0].length, a[1].length, a[2].length, ... Commented Oct 4, 2014 at 19:06
  • In a 2D array a[0] and a[1] refer to a row in the array. Typically, they should be the same length. This equally applies to a 3D array (although I would hesitate to call this a "row"). Commented Oct 4, 2014 at 19:08
  • @TienDoNam : that would be for the case when the rows have different sizes but the way you declared the array, they all have the same size Commented Oct 4, 2014 at 19:08
  • Eclipse Luna is not buggy. Well, actually, it is, but not because of this. This is the correct behaviour. Commented Oct 4, 2014 at 20:41

1 Answer 1

5

These outputs are perfectly correct. You have a 3-dimensional array of length 2 x 3 x 4 so for all valid indexes i, j :

  • a.length is 2
  • a[i].length is 3
  • a[i][j].length is 4
Sign up to request clarification or add additional context in comments.

2 Comments

stackoverflow.com/questions/7367218/… ... so i just miss unterstood, thank you
If you look closely the answer of this question, the array is not declared the way you did. The person who answered did not specify the number of columns per row, and then allocated separately the rows with different lengths. That's why he's getting those outputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.