*
The moose likes Beginning Java and the fly likes understanding output from a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "understanding output from a loop" Watch "understanding output from a loop" New topic
Author

understanding output from a loop

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i read same shuffling code from here and i dont get some things.
Here's my code:

here's the output:
gerado: 4
cartas[i]: Rei
temp: Rei
cartas[gerado]: Cinco
Sai: Cinco
gerado: 2
cartas[i]: Dama
temp: Dama
cartas[gerado]: Tr�s
Sai: Tr�s
gerado: 5
cartas[i]: Valete
temp: Valete
cartas[gerado]: Seis
Sai: Seis
gerado: 3
cartas[i]: Dez
temp: Dez
cartas[gerado]: Quatro
Sai: Quatro
gerado: 3
cartas[i]: Nove <---------------------HERE
temp: Nove
cartas[gerado]: Dez
Sai: Dez
gerado: 1
cartas[i]: Oito
temp: Oito
cartas[gerado]: Dois
Sai: Dois
gerado: 0
cartas[i]: Sete
temp: Sete
cartas[gerado]: �s
Sai: �s
gerado: 1
cartas[i]: Valete
temp: Valete
cartas[gerado]: Oito
Sai: Oito
gerado: 3
cartas[i]: Rei
temp: Rei
cartas[gerado]: Nove
Sai: Nove
gerado: 2
cartas[i]: Rei
temp: Rei
cartas[gerado]: Dama
Sai: Dama
gerado: 0
cartas[i]: Rei
temp: Rei
cartas[gerado]: Sete
Sai: Sete
gerado: 0
cartas[i]: Valete
temp: Valete
cartas[gerado]: Rei
Sai: Rei
gerado: 0
cartas[i]: Valete
temp: Valete
cartas[gerado]: Valete
Sai: Valete
I cant understand why such an output!!!
I expected "Quatro" again
I mean, on other words: how "cartas[gerado]" is generated?
can someone pls explain it to me?
tx in advance
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ February 15, 2004: Message edited by: Dirk Schreckmann ]

java amateur
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
If you're just after a shuffling routine, Collections.shuffle() should do what you want
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
that's easier then my code , but doesnt explain my doubt...
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
I think I see the answer to what you are asking, you are iterating from i = cartas.length to i = zero. In this case, cartas.length is equal to 13, the number of Strings in the array, so, the line:


Will print out first cartas[13], which is "Rei", then, the next time thru the loop, it will print cartas[12] which is "Dama", and so on. For the output line you have as the question:

cartas[i]: Nove <---------------------HERE

"i" is equal to 9, so the nineth element of the cartas[] array is returned,
"Nove".
Maybe it would be more clear to you if you printed out the value of "i" as you iterate from i=cartas.length to zero.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
good idea!
if i print the cards array everytime i advance in loop i can then see them beeing shuffled
thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: understanding output from a loop
 
Similar Threads
toString()...
Completed SCJP with 93%
[Easy] Shift String
Reading arrays
91%