Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

just java 2 p. 119

 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
Got some time to do some reading and wondered if I'd come upon a typo or just missed something. Here' s what's in the book:
------------------------------------
short [ ] h, i [ ], j, k[ ] [ ] ;
... In the code above, "j" is an array of short, and "i" is an array of arrays of short.
-------------------------------------

I understood the code to mean that "h" and "i" are arrays of short, "j" is a variable of type short, and "k" is an array of arrays of short.
Any hints?
thanks,
Pauline

 
Stuart Goss
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Pauline!
short [ ] h, i [ ], j, k[ ] [ ] ;
... In the code above, "j" is an array of short, and "i" is an array of arrays of short

I'd say
h is an array of shorts (1 dimensional)
i is an array of arrays of shorts (2 dimensional)
j is an array of shorts (1 dim.)
k is an array of arrays of arrays of shorts (3 dimensional)
What do the others think?
Stuart
 
Don Smathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I've read, there are no 'dimensions' in Java arrays.
That is, unlike C/C++ where anarray[][] means a two dimensional array where the number of rows and columns are = to some constant, Java employs arrays of arrays.
So, code like int[] i, j[], k[][] turns out to mean:
i is an array of int values.
j is an int array of int arrays (which can have unequal lengths).
k is an int array of int arrays of int arrays -- kind of weird, but legal --- all 3 can be of varying lengths.
Indexing is a nightmare for any except i.
Best bet seems to be k[][].length, etc.
LUCK !!
 
Amber Woods
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pauline,
I was thinking like you that j would be a single variable of type short not an array of type short. But could it be that since it reads with the [] right after the declaration of short that anything on that line is an array ??
I don't know the answer
Amber
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I agree with Pauline's interpretation.
 
Paul Ralph
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as she tripled checked to make sure it was typed (as in using a computer keyboard) correctly, I also agree with Pauline's interpretation.
Paul R
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my twisted way of looking at it.
short [] h, i[], j, k[][] ;
same as
short h[], i[][], j[], k[][][] ;
short [] h - an array of shorts named h. <-- some say this is really what it is and perfer
short [] h over short h[]
short [] h, i, j, k ; - arrays of shorts named h, i, j and k.
So adding one or more [] addes more arrays (deminsion) to the short arrays h, i, j and k.
short [] h, i[], j, k[][]; - h ,i, j and k are arrays of shorts with i being a short array of arrays and k being a short array of arrays of arrays.
If any of the above is wrong please say so. I'm still learning too
[This message has been edited by Richard Boren (edited May 11, 2001).]
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just went back to quadruple check the spelling/typing and think I found the solution. I had left out one important sentence when quoting the book that now makes things clearer for me.
Just as you thought Amber:
"If array brackets appear next to the type, they are part of the type, and apply to every variable in that declaration."
Looks like they didn't want to waste another line or two explaining it.
I sure wouldn't want to have to think about that nuance when confronted with code like that. Better to have a style guide that requires separate declarations for each array.
So, the question for java gurus out there:
Would the eqivalent of

short [ ] h, i [ ], j, k[ ] [ ] ;
be something like this:
short h[ ] ;
short i[ ] [ ] ;
short j[ ] ;
short k[ ] [ ] [ ] ;
???
thanks for the help
Pauline

[This message has been edited by Pauline McNamara (edited May 11, 2001).]
 
Chris Caillouet
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pauline,
Yes, I think that's right...just as Stuart, Don, and Richard originally surmised. The brackets after the type mean that every identifier which follows will refer to an array of that type. Putting additional [] after an identifier in this case makes it an array of arrays (...of array, as in the case of k!) of that type.
I agree that the indexing gets to be nightmarish, but I also happen to be a bit fond of multi-dimensional arrays (or arrays of arrays) -- I'm funny that way!
Chris
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Pauline. Your last explanation is the correct one.

short[] h, i[], j, k[][] ;

is equivalent to

short[] h ;
short[] i[] ;
short[] j ;
short[] k[][] ;

which is equivalent to

short h[] ;
short i[][] ;
short j[] ;
short k[][][] ;

at least as far as the syntax of the language is concerned. But, as you noted, it certainly seems less readable.

The JLS (Java� Language Specification) states
"The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both ..."

Java supports the short h[] form for the benefit of C/C++ programmers. However, the short[] h form better represents the true nature of arrays in Java.
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Pauline I learnt something new
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you , Johannes, and everyone else, too. It helps a lot to think through those things with other folks, and besides it's more fun than reading alone.

Pauline
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thank you Marilyn.
I was wondering if you, or someone else, could expound on this a little:
Originally posted by Marilyn deQueiroz:


Java supports the short h[] form for the benefit of C/C++ programmers. However, the short[] h form better represents the true nature of arrays in Java.

How does that form better represent the true nature of Java arrays? (Maybe that's obvious for the c/c++ programmers out there, but for me... )
happy mother's day all
Pauline
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The long[] h form better represents the true nature of arrays in Java because it is clearly an array of "long"s. In other words, it visually links the array to the type rather than to the identifier.

However, in C and C++, the programmer has no other option than to put the square brackets after the identifier, so that is what they are familiar with. Therefore, the designers of Java decided to allow that form as well.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great!
thanks Marilyn.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic