• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random question about Array from SCJP  RSS feed

 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me the meaning of this two lines :

int x=(int) (Math.random() *5 );


String name= new String[] { "a","b","c","d"} [x];
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first creates a random number.

The second creates an array.

Did you have a more specific question?
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to say that I don't understand the second line, either - what does the [x] at the end do, exactly?

Whoops, okay. The second line does not create an array. It takes the xth String from the array before.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this thread for the bit about random numbers.
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the code :

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kavi lathigra wrote: . . . String name= new String[] { "a","b","c","d"} [x] . . .
It would have been easier if you had started by working out the code not struck out creates a String array.

And please always tell us where such code comes from.
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, I already mentioned it " the code comes from SCJP book"
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always use code tags and don't double‑space code. I'll sort it out this time, and you can see how much easier it is to read.

And please tell us what sort of Exception you can expect
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kavi lathigra wrote:this is the code :

Class name Animal, surely, or it won't compile?
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it compiles!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kavi lathigra wrote:it compiles!



Campbell meant the code that you posted, and not the code that you cut-n-pasted from... because the code that you posted certainly will not compile.

Henry
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably!
It works fine, but I am just getting an exception : ArrayOutOfBound Exception
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kavi lathigra wrote:probably!
It works fine, but I am just getting an exception : ArrayOutOfBound Exception


Do you know why you are getting this?

Hint: Look at these lines.
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!
these two line are nothing but a shortcut .

it's like :

String nameList= {"a","b","c","d");
String name=nameList[x];

It retrieves the string at index [x] ( x being the random number generated )
of the newly created array.

Thank you all~!
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remind yourself of this; you have introduced all sorts of interesting errors into that code.
No, you did not say which SCJP book that code came from; you only said “SCJP”. Is it Sierra and Bates?

As for the Exception
Earlier, I wrote: . . . please tell us what sort of Exception you can expect
You heard it first on this channel
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology for that,

the question I asked you regarding "array" was from "Sun Certified Java Programmer " book , written by " Katty Sierra" and "Bert Bates"
Mc Graw Hill Education Edition 2008

Thank you for making me aware about rules .
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!