Java使用for循环写入多个数组列表

Java使用for循环写入多个数组列表,java,arrays,loops,for-loop,Java,Arrays,Loops,For Loop,我正试图用java实现这一点。。。基本上是通过6个人循环,然后让他们滚动100个随机数, 只是尝试更有效地循环数组列表名称,而不是为不同的人使用6个循环。谢谢你的帮助 int min = 1; int max = 6; int range = (max - min + 1); ArrayList person1 = new ArrayList(); ArrayList person2 = new ArrayList(); ArrayList person3 = new ArrayList();

我正试图用java实现这一点。。。基本上是通过6个人循环,然后让他们滚动100个随机数, 只是尝试更有效地循环数组列表名称,而不是为不同的人使用6个循环。谢谢你的帮助

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);
    
intmin=1;
int max=6;
整数范围=(最大-最小+1);
ArrayList person1=新的ArrayList();
ArrayList person2=新的ArrayList();
ArrayList person3=新的ArrayList();
ArrayList person4=新的ArrayList();
ArrayList person5=新的ArrayList();
ArrayList person6=新的ArrayList();

对于(inti=1;i,根据Federico的评论,您可以在
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);
}
说明

IntStream.range(0,6)
生成从0(包括0)到6(排除)的
int流,即
0,1,2,3,4,5
。 我们使用
mapToObj
使用
mapper
i->…
)将它们转换成一个列表,最后我们将
收集成一个新列表


在映射器内部,我们使用另一个
IntStream
生成100个随机值,我们再次将其收集到
列表中

如果您使用一个
ArrayList
s的数组而不是6个独立的
ArrayList
s,则会更容易。太好了,谢谢!太好了,谢谢!
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);