0

Im trying to achieve this in java... basically looping through 6 people then getting them to roll 100 random numbers, Just trying to loop through the array list names more efficiently rather than having 6 loops for the different people. Any help appreciated.

int min = 1;
int max = 6;
int range = (max - min + 1);
ArrayList person1 = new ArrayList();
ArrayList person2 = new ArrayList();
ArrayList person3 = new ArrayList();
ArrayList person4 = new ArrayList();
ArrayList person5 = new ArrayList();
ArrayList person6 = new ArrayList();

for (int i = 1; i <=6; i++) {
    for (int j = 0; j <100; j++) {
        int rand = (int) (Math.random() * range) + min;
        //            System.out.print(rand + ", ");
        String person = "person" + i;
        System.out.println(person);
        person.add(rand);
            
    }
    
}

System.out.println(person1);
System.out.println(person2);
System.out.println(person3);
System.out.println(person4);
System.out.println(person5);
System.out.println(person6);
    
1
  • 3
    It would be easier if, instead of 6 separate ArrayLists, you used an array of ArrayLists. Commented Nov 12, 2020 at 10:41

1 Answer 1

1

As per Federico's comment, you can loop over an array (or List) of ArrayList:

ArrayList[] persons = new ArrayList[6];

for(int i=0; i<6; i++) {
    ArrayList<Integer> person = new ArrayList<>(100);
    
    for(int j=0; j<100; j++) {
        int rand = (int) (Math.random() * range) + min;
        //            System.out.print(rand + ", ");
        person.add(rand);
    }
    persons.add(person);
    
    System.out.println(person);
}

Please note that this involves a lot of auto boxing (implicit conversion from int to Integer), so if the 100 count is fixed, but the number of people may increase, it may become more efficient to use int[] instead of ArrayList to store the random numbers.

If you like streams, you can also do this:

List<List<Integer>> persons = IntStream.range(0,6)
        .mapToObj(i -> 
                  IntStream.range(0,100)
                            .map(j -> (int) (Math.random() * range) + min)
                            .mapToObj(Integer::valueOf)
                            .collect(Collectors.toList()) //this is the list of 100 random numbers
                )
        .collect(Collectors.toList()) //to produce the list of 6 person lists
        ;
//print, return, do anything
persons.forEach(System.out::println);

Explanation:

IntStream.range(0,6) produces a Stream of ints from 0 inclusive to 6 exclusive, i.e. 0,1,2,3,4,5. We use mapToObj to turn each of these into a list using a mapper (i -> ...), which we finally collect into a new list.

Inside the mapper, we use another IntStream to produce the 100 random values, which again we collect into a List.

Sign up to request clarification or add additional context in comments.

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.