• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
Python Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic