4

How would I select random elements from an array list, except for one element? Here's my arraylist:

ArrayList <String> provinces = new ArrayList();

Collections.addAll(
    provinces, 
    "New Brunswick", 
    "Saskatchewan", 
    "Ontario", 
    "Nova Scotia", 
    "Quebec", 
    "Alberta");

For this example, I would like to select at random the other elements, except for Saskatchewan.

I've tried doing:

 for(int i == provinces.get (0); i < provinces.get(1); i > provinces.get(2); i < provinces.get(5)) {

     int getPossibleAnswers = (int) Math.floor(Math.random()*i);               
     String displayPossibleAnswers = provinces.get(getPossibleAnswers);                
     outputAnswers.append(displayPossibleAnswers + "\n");
     }

Obviously, this code doesn't work, and I don't know what to do. Thanks in advance!

2
  • but how many elements do you need? Commented Jun 8, 2017 at 17:13
  • 1
    why don't you simply create a list without Saskatchewan and apply Collections.shuffle on it Commented Jun 8, 2017 at 17:14

4 Answers 4

4

Build a list of all the index values, except for the index to Saskatchewan, then shuffle that.

List<String> provinces = Arrays.asList("New Brunswick", "Saskatchewan", "Ontario", "Nova Scotia", "Quebec", "Alberta");

List<Integer> indexes = new ArrayList<>();
for (int i = 0; i < provinces.size(); i++)
    if (! provinces.get(i).equals("Saskatchewan"))
        indexes.add(i);
Collections.shuffle(indexes);

StringBuilder outputAnswers = new StringBuilder();
for (int i : indexes)
    outputAnswers.append(provinces.get(i) + "\n");

System.out.print(outputAnswers);

Sample Output

Nova Scotia
Quebec
Ontario
Alberta
New Brunswick
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, sort of new to Java, what does the colon (:) do?
@PendleS That is the enhanced for loop, also known as the foreach loop. See: What is the syntax of enhanced for loop in Java?
Thanks you for the answers
1

To keep it concise , As mentioned Simply create a list without Saskatchewan

    ArrayList <String> provinces = new ArrayList<>(Arrays.asList("New Brunswick",
                                     "Ontario", "Nova Scotia",
                                     "Quebec",   "Alberta"));

then shuffle the list

Collections.shuffle(provinces);

Demo

ArrayList <String> provinces = new ArrayList<>(Arrays.asList("New Brunswick",
                                     "Ontario", "Nova Scotia",
                                     "Quebec",   "Alberta"));
Collections.shuffle(provinces);
StringBuilder builder = new StringBuilder();
for (String s:provinces) {
    builder.append(s+"\n");
}
System.out.println(builder);

Output :

Quebec
New Brunswick
Alberta
Ontario
Nova Scotia

Comments

0

If you need randomization often (e.g. for randomized testing), you can add a Qala Datagen dependency and then get a random element like this:

String e = sample(provinces);

Though you'd still need to remove extra element from the collection.

Disclaimer: I'm the author of the lib.

Comments

0
ArrayList <String> provinces = new ArrayList<>();
String removeValue = null;
Collections.addAll(
    provinces, 
    "New Brunswick", 
    "Saskatchewan", 
    "Ontario", 
    "Nova Scotia", 
    "Quebec", 
    "Alberta");

for (Iterator<String> iterator = provinces.iterator(); iterator.hasNext(); ) {
    String value = iterator.next();
    if (value.equals("Saskatchewan")) {
        removeValue = value;
        iterator.remove();
    }
}

Collections.shuffle(provinces);

for(String p: provinces){
    System.out.println(p);
}

provinces.add(removeValue);

I used Iterator because enhanced for loop did not work with remove() but without Iterator you can also loop using indexes(numbers).

Without Iterator:

for (int j = 0; j<provinces.size(); j++) {
    if(provinces.get(j).equals("Saskatchewan")){
        removeValue = provinces.get(j);
        provinces.remove(provinces.get(j));
    }
}

Also without using Collections.shuffle() you can create your own method. To do that have a look at this question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.