• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array question

 
M Mangion
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have 10 names which I am storing in an array of type string, and now using a random function I would like to create 5 matches as follows:

name2 vs name4
name3 vs name6
name1 vs name5
etc

what is the best way to achieve this ?

Thank you.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach - shuffle the array (there is a method in the JDK to do this but you could write your own which is probably what your teacher wants you do do) then use the values at index i and i+1 (i even) as a pair.
 
Mikael Romo
Greenhorn
Posts: 6
Java Python Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!



i'm not telling you that this is the best way to achieve your goal. but its working. Maybe wait for another suggestions.. Hehe!
God bless you..
 
xinghai huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sabre wrote:One approach - shuffle the array (there is a method in the JDK to do this but you could write your own which is probably what your teacher wants you do do) then use the values at index i and i+1 (i even) as a pair.




or

 
M Mangion
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thank you very much for your replies. Regarding the first post, thanks for the suggesting about the shuffle of the array, I am reading about it.

As regards the 2nd post, thank you for the sample code, actually I had already created something along those lines, but the problem is that the same team can be used more than one match, and thus not all teams are being used.

Thank you
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that could pair A with F, then again A with G - the random element can be reselected. A similar approach would be to use a List<String>, then remove the random elements instead of only retrieving them.

And welcome to the Ranch!


James, unfortunately, the shuffle method is only available for Lists, not for arrays. Of course you can wrap the array using Arrays.asList and then shuffle that.

And I'm too slow today - M Mangion addressed my first issue, xinghai huang addressed the second...
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:

James, unfortunately, the shuffle method is only available for Lists, not for arrays. Of course you can wrap the array using Arrays.asList and then shuffle that.



I had forgotten that but my post was meant to push the OP in the right direction but it seems that others want to actually do the OP's homework for him.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic