aspose file tools*
The moose likes Cattle Drive and the fly likes just java 2 p. 119 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "just java 2 p. 119" Watch "just java 2 p. 119" New topic
Author

just java 2 p. 119

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Mar 21, 2001
Posts: 171
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

Joined: Mar 04, 2001
Posts: 31
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

Joined: Mar 28, 2001
Posts: 111
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


"Happiness is a way to Travel, <b>Not</b> a Destination" -- Unknown
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Personally I agree with Pauline's interpretation.
Paul Ralph
Ranch Hand

Joined: Aug 10, 2000
Posts: 312
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

Joined: Mar 01, 2001
Posts: 233
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

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Apr 27, 2001
Posts: 7
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

Joined: Jul 22, 2000
Posts: 9043
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Thank you Pauline I learnt something new
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Jul 22, 2000
Posts: 9043
    
  10
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

Joined: Jan 19, 2001
Posts: 4012
    
    6
great!
thanks Marilyn.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: just java 2 p. 119
 
Similar Threads
UrlyBird : ArrayList problem
Label control doubt??
Multi Dimension Array Storing
ArrayList
Converting a two dimensional int array into a byte array