• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

construct 2 dimensional array

 
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.10.1
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



 
Richard Hayward
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for your comments.

Regards
Richard
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
>
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic