Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

An odd array phenomenon  RSS feed

 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dirk Schreckmann:
thanks again for the great illustration,i got it.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure sure I understand how that code is legal (of course it is!).
I would have written:

Is this equivalent?
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!