Reputation: 103
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!
Upvotes: 4
Views: 1983
Reputation: 22447
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.
Upvotes: 0
Reputation: 15316
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.
Upvotes: 0
Reputation: 37404
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
Upvotes: 1
Reputation: 159185
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
Upvotes: 4