954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A Question on Arrays

In the Code below i have a program using multi dimensional arrays. I have a quick question on "How to" ad a checking system so my random class does not generate the same "type" of horse twice. For instance right now i have my program pick a random color with the name stallion i do not want it to pick blue twice

Main Class

String[][] horses = {{"Black ","White ","Gray ","Old ","Young ",
                             "Pink ","Yellow ","Brown ","Orange ","Cyan ", 
                             "Purple ","Tan ","Cerulean ","Silver ","Gold ", 
                             "Red ","Green ","Blue ","Maroon ","Marigold ", 
                             "Iron ","Paper ","Wood ","Zinc ","Metal "},
                             {"Stallion"}
                           };
                         
      String[][] horsePlace = {{pickRand.get(horses[0]), pickRand.get(horses[0]),  pickRand.get(horses[0])
                              , pickRand.get(horses[0]), pickRand.get(horses[0]), pickRand.get(horses[0])},
                              {pickRand.get(horses[1]), pickRand.get(horses[1]), pickRand.get(horses[1]),
                               pickRand.get(horses[1]), pickRand.get(horses[1])}};


Random Class

import java.util.Random;
class pickRand {
    public static String get (String[] array) {
        Random generator = new Random();
        int rnd = generator.nextInt(array.length);
        return array[rnd];
    }
}
Eragah
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 1
 

keep an array, or a list with types you've already added.
when you create a new one, check in that array or list whether it already exists, if not, add it to the list, if it does, ignore it and create a new one.

for future reference, when you say: this is my main method, you may want to show the complete code you have in the main method, that might be easier for us.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: