This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes missing dimension Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "missing dimension" Watch "missing dimension" New topic

missing dimension

jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
I tried to compile the below code and i get "array dimension missing" error

But if I compile the following code, it gets compiled. Anyone please explain why don't i get missing dimension error this time?

Still Learing..
Sergio Calvo

Joined: Feb 22, 2008
Posts: 8
That's because the way in which arrays work in Java.

The Java compiler always, always needs to know how much space to take for your Array when you are creating it (with "new").

In the first case, int[] arr = new int[];

The compiler doesn't have that data, so it will always, always, fail. It doesn't know how much space do you actually need.

But, in the second case, there's a subtle difference,

int[][] arr = new int[5][];

The compiler actually has the data it needs, it needs 5 spaces for the new array (the first dimension). That's all it needs.

The second dimension doesn't matter at this point since it is another set of different arrays. So the compiler will happily continue with no problems.

But, you will actually need to make that initialization when you try to use it, say for example:

arr[0] = new int [2]; //arr[0] is going to be another array with two ints.

arr[0][0] = 50; //the first int in array[0] is 50;
arr[0][1] = 100; //the second int in array[0] is 100;

Does it makes sense?
I agree. Here's the link:
subject: missing dimension
jQuery in Action, 3rd edition