Select multiple / random items from arraylist

How to select random elements from a list of arrays except element one ? Here is my arraialist:

ArrayList <String> provinces = new ArrayList();

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

In this example, I would like to randomly select items other than Saskatchewan.

I tried:

 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 does not work, and I do not know what to do. Thanks in advance!

+4
source share
4 answers

Create a list of all index values ​​except the Saskatchewan index, then shuffle it.

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);

Output example

Nova Scotia
Quebec
Ontario
Alberta
New Brunswick
+4
source

To keep it concise, simply indicate listwithoutSaskatchewan

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

then shuffle 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
+1
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);

Iterator, for remove(), Iterator ().

Iterator:

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

Collections.shuffle() . .

0

(, ), Qala Datagen, ,

String e = sample(provinces);

.

: lib.

0

Source: https://habr.com/ru/post/1678810/


All Articles