soccerway soccerway - 1 month ago 10
Java Question

Array are not selecting values randomly and split into two arrays


I have got an array list of animals, on click on 'select' button I
would like to randomly select these animals and pass animals into two
arrays (split)
called 'teamA and teamB'. Here is my code, but I am getting the same array list always as per screenhot link ? Could someone please
help me to figure out the problem ?


import java.lang.Math;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java.util.Random;

public class RandomExample {

private Random random = new Random();

public static void main(String[] args) {
// 'list' array list contains animals
List<String> list = new ArrayList<String>();
list.add("Tiger");
list.add("Crocodile");
list.add("Cat");
list.add("Dog");
list.add("Elephant");
list.add("Lion");
list.add("Deer");
list.add("Eagle");

RandomExample obj = new RandomExample();
for(int i = 0; i < 10; i++){
obj.getRandomList(list);
List<String> teamA = list.subList(0, 4);
List<String> teamB = list.subList(4, 8);
System.out.println(teamA);
System.out.println(teamB);

}

}

public String getRandomList(List<String> list) {

//0-4
int index = random.nextInt(list.size());
System.out.println("\nIndex :" + index );
return list.get(index);

}

}


[![enter image description here][1]][1]


[1]: https://i.stack.imgur.com/Hz1Ol.jpg

Answer

As I asked I am not sure why you do not capture the returned string from getRandomList()… I am guessing you are thinking that the list gets returned? Another issue is that when you get a random number from the list you could get the same number. Therefore you will possibly get the same animal on both teams or even the same animal twice or more on the same team. When you put an animal on a team… you need to remove them from the list.

Below I create the two teams. Then setup two loops, one for each team. Using your getRandomList method to get a random animal then remove that animal from the list. After we have both lists, print the results. Hope this helps.

public class Main
{
  private static Random random = new Random();

  public static void main(String[] args)
  {
      // 'list' array list contains animals
      List<String> list = new ArrayList<String>();
      list.add("Tiger");
      list.add("Crocodile");
      list.add("Cat");
      list.add("Dog");
      list.add("Elephant");
      list.add("Lion");
      list.add("Deer");
      list.add("Eagle");

      List<String> teamA = new ArrayList<String>();
      List<String> teamB = new ArrayList<String>();
      String newAnimal;
      for(int i = 0; i < 4; i++)
      {
        newAnimal = getRandomList(list);
        teamA.add(newAnimal);
        list.remove(newAnimal);
      }
      for(int i = 0; i < 4; i++)
      {
        newAnimal = getRandomList(list);
        teamB.add(newAnimal);
        list.remove(newAnimal);
      }
      System.out.println(teamA); 
      System.out.println(teamB);             
  }

  public static String getRandomList(List<String> list) {

      //0-4
      if (list.size() > 1)
      {
          int index = random.nextInt(list.size());
          //System.out.println("\nIndex :" + index ); 
          return list.get(index);
      }
      else
      {
          return list.get(0);
      }

  } 
}