Home > Software engineering >  How can I randomly pull out a String from an array list and put it in another?
How can I randomly pull out a String from an array list and put it in another?

Time:04-08

As seen in the code that I tried to write, I want to grab a name from the Array list "names" and either add it to groupA or B. I do not need the groups to be even.

import java.io.File;
import java.io.FileNotFoundException; 
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;

public class GroupPicker{
    public static void main(String[] args) throws FileNotFoundException{
        Scanner input = new Scanner(new File("GroupPicker1.txt"));
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<String> groupA = new ArrayList<String>();
        ArrayList<String> groupB = new ArrayList<String>();
        Random random = new Random();
        while(input.hasNext()){
            String nextName = input.next();
            names.add(nextName);
        }
        for (int i = 0; i < names.size(); i  ) {
          names.get(random.nextInt(groupA.add()));
          
          names.get(random.nextInt(groupB.add()));
        }
        System.out.println( "\n"   "project groups: ");
        System.out.println("Group A: "   groupA);
        System.out.println("Group B: "   groupB);   
    }
}

CodePudding user response:

When I read this problem, two conditions stand out.

  1. the names must go into one of the lists (meaning not the other) -- you must take it from the names ArrayList
  2. the names do not need to be even -- you do not need to keep track of which list they are being added to

I would use an enhanced for-loop for ease of use (the less variables the better)

    // this cycles through each index of the names ArrayList
    for(String singleName : names)
    {
        // creates a random number
        java.util.Random number = new java.util.Random();

        // add to groupA if it is an even number and groupB if it is odd
        if(number.nextInt(2) % 2 == 0)
            groupA.add(singleName);
        else
            groupB.add(singleName);
    }

EDIT AFTER SHOWING MY PROFF XD

For simplicities sake, you could even cut off the % 2 == 0 because 0 and 1 will only ever give you 0 or 1 with that statement. This could look like this:

    // true = one list && false = the other
    if(number.nextBoolean())
        groupA.add(singleName);
    else
        groupB.add(singleName);

CodePudding user response:

First you have to make sure the random.nextInt() doesn't give you a too high value, because you only put so much values into names, but random.nextInt() will return any int - this could be even negative!

So the easiest way would be this:

  1. shuffle the names list with Collections.shuffle(names)
  2. put the first half of names into groupA - with the use of List.subList you would not even have to create a new list, just use the returned result.
  3. put the second half of names into groupB

You have to take care about the indexes for the "halfes" - and decide what to do if the number of entries in names is odd - put one more into groupA or groupB.

CodePudding user response:

How can I randomly pull out a String from an array list and put it in another

List<String> names = new ArrayList<>();
Random r = new Random();
  • nextInt(n) - returns a value between 0 and n-1 inclusive
  • r.nextInt(names.size()) - will get one random index of all possible indices
String name = names.get(r.nextInt(names.size());
List<String> otherList = new ArrayList<>();
otherList.add(name);

Note you can also shuffle a list if required.

Collections.shuffle(names);

Then you can simply iterate across the list, getting names in the new random order.

CodePudding user response:

This is what I would do if I understand your need :


// Scanner input = new Scanner(new File("GroupPicker1.txt"));
      String arr[] = new String[] { "A", "B", "C", "D" };
      ArrayList<String> names = new ArrayList(Arrays.asList(arr));
      ArrayList<String> groupA = new ArrayList<String>();
      ArrayList<String> groupB = new ArrayList<String>();
      
      // while(input.hasNext()){
      //     String nextName = input.next();
      //     names.add(nextName);
      // }
      while (!names.isEmpty()) {
        for (int i = 0; i < names.size(); i  ) {
          int random = ThreadLocalRandom.current().nextInt(i, names.size());
          boolean randBool = Math.random() < 0.5;
          String name = names.get(random);
          names.remove(random);
          if (randBool){ 
            groupA.add(name);
            break;
          };
          groupB.add(name);
        }  
      }
      
      System.out.println( "\n"   "project groups: ");
      System.out.println("Group A: "   groupA);
      System.out.println("Group B: "   groupB); 
  •  Tags:  
  • java
  • Related