my dog learned polymorphism*
The moose likes Beginning Java and the fly likes array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array question" Watch "array question" New topic
Author

array question

M Mangion
Greenhorn

Joined: Jun 06, 2011
Posts: 11
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

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Mikael Romo
Greenhorn

Joined: Jun 14, 2011
Posts: 6

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

Joined: Jun 17, 2011
Posts: 4
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

Joined: Jun 06, 2011
Posts: 11
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

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...


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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.
 
subject: array question
 
Similar Threads
for loop vs while loop
Vector Vs. LinkList Vs. ArrayList Vs. Collection Vs. HashTable
System.arraycopy VS toArray()
ArrayList vs String[]
() vs []