aspose file tools*
The moose likes Beginning Java and the fly likes construct 2 dimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "construct 2 dimensional array" Watch "construct 2 dimensional array" New topic
Author

construct 2 dimensional array

Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
To declare and create an array that could take 3 Dogs:

Dog[] d = new Dog[3]

although no dogs exist yet.

Following that same pattern, to create an array that
could hold 3 int[] objects, I would expect

int[][] a = new int[][3]

where I'm saying that I want to create an array on the heap,
with 3 elements, each element being a reference to an int[] that
doesn't exist yet.

But this won't compile. The rules for creating multidimensional arrays
where only one dimension is specified require

int[][] a = new int[3][]

Is the rule for denoting an array of x ,
where brackets are added immediately to the right of x ie x[]
not consistent throughout java?


Regards

Richard
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Dog[] d = new Dog[3]


As said there will be 3 entries in "d" and all pointing to null.

int[][] a = new int[][3]

This is an error because the compiler isn't able to figure out the size of the array. In the first case you specified 3 as the size of the array. But in this case you aren't specifying the size of the array. The 3 indicates the size of the individual array which the array would contain.
Showing it conceptually:
a -> [[array1],[array2],[array3]...] where each array1|array2 is of size 3. But the compiler is looking for the size of "a" and not that of individual arrays like array1|array2 and so on.

Whereas,
int[][] a = new int[3][]

showing it conceptually:
a -> [[array1],[array2],[array3]] where the size of "a" is fixed as 3 but the size of individual arrays can vary. Each element of "a" is nothing but the address of where that element is present. So if you dont define "a" as int[3][] then the compiler is not aware of how many elements would be present in "a". Hence int[3][] is valid and int[][3] is not.


Mohamed Sanaulla | My Blog
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.10.1
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
There’s no such thing as a 2D array. Only an array of arrays. int[][] numbers = new int[3][]; means ”I want numbers to be an array holding three int[] arrays, which do not yet exist.”
Have a look at what Fred Rosenberger says about egg boxes, which makes a nice illustration of arrays.
Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
Campbell Ritchie wrote: int[][] numbers = new int[3][]; means I want numbers to be an array holding three int[] arrays, which do not yet exist.


Yes. So the syntax seems inconsistent.

If I want an array holding 3 Dog objects that don't yet exist, I use

new Dog[3]

the [3] comes after the Dog datatype.

If I want an array holding 3 int[] objects that don't yet exist, I have to use

new int[3][]

where the [3] is inserted in the middle.


Regards
Richard
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Guy Hayward wrote:To declare and create an array that could take 3 Dogs:

Dog[] d = new Dog[3]

although no dogs exist yet.

Following that same pattern, to create an array that
could hold 3 int[] objects, I would expect

int[][] a = new int[][3]

where I'm saying that I want to create an array on the heap,
with 3 elements, each element being a reference to an int[] that
doesn't exist yet.

But this won't compile. The rules for creating multidimensional arrays
where only one dimension is specified require

int[][] a = new int[3][]

Is the rule for denoting an array of x ,
where brackets are added immediately to the right of x ie x[]
not consistent throughout java?



Interestingly, Java allows both ways to declare an array. It can be done like...



or like ...



and unless you are declaring more than one variable with the same line, these two syntaxs are essentially interchangable.


Unfortunately, both of these syntax can't be supported with the "2D array" syntax. So, depending on which syntax you are comfortable with, I guess you can argue that it is either consistent or non-consistent. Personally, I say it doesn't matter, as I can argue that neither syntax implies how an additional dimension is to be added -- any assumption is flawed. It has to be specified, and this is how the JLS defines it.

Henry




Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
Thanks to all for your comments.

Regards
Richard
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2409
    
  28

Guy Hayward wrote:
Campbell Ritchie wrote: int[][] numbers = new int[3][]; means I want numbers to be an array holding three int[] arrays, which do not yet exist.


Yes. So the syntax seems inconsistent.

If I want an array holding 3 Dog objects that don't yet exist, I use

new Dog[3]

the [3] comes after the Dog datatype.

If I want an array holding 3 int[] objects that don't yet exist, I have to use

new int[3][]

where the [3] is inserted in the middle.


Regards
Richard


Ahh I see what your confusion is.

What you are saying is if you declare an array of 3 Animals, you declare it like this



Now let's say you decide that all Animals are really a collection of multiple legs. Some animals will have 2 legs, some will have 4. So you should be able to replace Dog with Leg[]



However, the way you really do is




Right? Right?
I think what you are saying makes sense for the point of view of language design.

To answer your question, the reason why they did this is because most programming languages do it like this and all of them do it this way to be compatible with mathematical array notations

Generally in Matrix algebra, A(i,j) denotes the the ith row and jth column of matrix A. Java models this as A[i][j]

They could have done it so you have to denote A(i,j) as A [j][i], but you will end up confusing a lot of mathematicians, and early programmers were all mathematicians . They just stuck with the notation that they were comfortable with

In short, it is this way because it made sense to people who created programming languages
Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
Jayesh A Lalwani wrote:

Right? Right?
I think what you are saying makes sense for the point of view of language design.

To answer your question, the reason why they did this is because most programming languages do it like this and all of them do it this way to be compatible with mathematical array notations

Generally in Matrix algebra, A(i,j) denotes the the ith row and jth column of matrix A. Java models this as A[i][j]

They could have done it so you have to denote A(i,j) as A [j][i], but you will end up confusing a lot of mathematicians, and early programmers were all mathematicians . They just stuck with the notation that they were comfortable with

In short, it is this way because it made sense to people who created programming languages


Thank you for your reply Jayesh.
Yep. That's what was bothering me.

Since originally posting, I've been trying to think through what the implication would be if the syntax for the declaration of multi dimensional arrays were as I originally thought it should be.



At line 7, declaring and instantiating the pets array would have to become



but getting to a particular pet would have to remain



so which dimension was which would be different for the purpose of accessing elements than for creating the array. That would be far more confusing than the issue I was originally complaining about!

So after all, I'm now entirely happy with the array creation syntax as it is.


Thanks again for your help.

Regards
Richard
>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: construct 2 dimensional array