# understanding output from a loop

miguel lisboa
Ranch Hand
Posts: 1281
i read same shuffling code from here and i dont get some things.
Here's my code:

here's the output:
cartas[i]: Rei
temp: Rei
Sai: Cinco
cartas[i]: Dama
temp: Dama
Sai: Tr�s
cartas[i]: Valete
temp: Valete
Sai: Seis
cartas[i]: Dez
temp: Dez
Sai: Quatro
cartas[i]: Nove <---------------------HERE
temp: Nove
Sai: Dez
cartas[i]: Oito
temp: Oito
Sai: Dois
cartas[i]: Sete
temp: Sete
Sai: �s
cartas[i]: Valete
temp: Valete
Sai: Oito
cartas[i]: Rei
temp: Rei
Sai: Nove
cartas[i]: Rei
temp: Rei
Sai: Dama
cartas[i]: Rei
temp: Rei
Sai: Sete
cartas[i]: Valete
temp: Valete
Sai: Rei
cartas[i]: Valete
temp: 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?
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ February 15, 2004: Message edited by: Dirk Schreckmann ]

Michael Dunn
Ranch Hand
Posts: 4632
If you're just after a shuffling routine, Collections.shuffle() should do what you want

miguel lisboa
Ranch Hand
Posts: 1281
that's easier then my code , but doesnt explain my doubt...

Kim Kantola
Ranch Hand
Posts: 276
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
Posts: 1281
good idea!
if i print the cards array everytime i advance in loop i can then see them beeing shuffled
thanks