| Author |
An odd array phenomenon
|
Peng Fan
Ranch Hand
Joined: May 04, 2004
Posts: 35
|
|
would you please explain this segment of code which made me depressed) ================================start======================================= String transporationTools = new String[] {"Tube","Bus","Railway","Bicycle"}[new Random.nextInt(4)]; ==================================end======================================= the right part of the assignment operator seem to be a two-dimension array,but when I take a close look at the left part and run this code I know that's impossible,but what makes me crazy is the right part,what the hell that is.Your detailed explanation will be highly appreciated! [ edited to add line break -ds ] [ May 04, 2004: Message edited by: Dirk Schreckmann ]
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
new String[] {"Tube","Bus","Railway","Bicycle"} creates a new array object. It's a String array with four components - the strings "Tube", "Bus", "Railway", and "Bicycle". That square bracket part, [new Random.nextInt(4)], is concerned with referencing a component in the array, at a randomly selected index. (If the use of a Random object is unfamiliar territory, perhaps you'd appreciate a look at the Random class documentation.) The above explanation is fine and dandy, but the better lesson to pull from this is don't write ugly, unreadable, odd code like this. Strive to write clear code that expresses well its meaning to the humans that read it. So, perhaps a preferred rewrite of this code would be the following. String[] selections = new String[] {"Tube","Bus","Railway","Bicycle"}; int index = new Random.nextInt(4); String transportationTools = selections[index]; [ May 04, 2004: Message edited by: Dirk Schreckmann ]
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Peng Fan
Ranch Hand
Joined: May 04, 2004
Posts: 35
|
|
Dirk Schreckmann: thank you for you answer,but another question is whether whether this handy and compact skill can be applied to two-dim or three-dim or even greater array,say,String str = new string[]{"usa,"uk","mexico","china"}[]{"washington","london","mexico city","beijing"}[1][1];Is this code doable? or whether this kind reduction is logical?THX!
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
String str = new string[]{"usa,"uk","mexico","china"}[]{"washington","london","mexico city","beijing"}[1][1];Is this code doable? Let me suggest that a very good answer to questions of this type can often be discovered by simply writing a simple test program to see what happens. If you were to do this, you'd discover that the example has a few compilation errors. The proper syntax to do such a thing as I believe you're trying to express is as follows. But as I suggested above, that's just ugly, and I'd assume that more than one experienced programmer would need to pause and think when trying to understand that small bit of code. Strive to write clear code that expresses well its meaning to the humans that will read it.
|
 |
Peng Fan
Ranch Hand
Joined: May 04, 2004
Posts: 35
|
|
Dirk Schreckmann: thanks again for the great illustration,i got it.
|
 |
Eddie Vanda
Ranch Hand
Joined: Mar 18, 2003
Posts: 281
|
|
I am not sure sure I understand how that code is legal (of course it is!). I would have written: Is this equivalent?
|
The nice thing about Standards is that there are so many to choose from!
|
 |
Peng Fan
Ranch Hand
Joined: May 04, 2004
Posts: 35
|
|
hi,Eddie Vanda: i don't think your code is equivalent to mine. yours is just to produce a two-dimension array of type String,you got a reference to that array objcet, while the code written by me is just to generate a variable of type String which was assigned with reference to an array Object of type String.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
So, in my last example, don't forget to notice the [1][1] part, where a component in the two dimensional array is referenced. If you changed it as follows, your example would be equivalent, except that this maintains a reference to the two dimensional array, where the previous examples don't. [ May 07, 2004: Message edited by: Dirk Schreckmann ]
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Eddie Vanda: I am not sure sure I understand how that code is legal (of course it is!). I would have written: Is this equivalent?
Your code is shortcut for
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: An odd array phenomenon
|
|
|