代码:
package com.company;
import java.util.ArrayList;
import java.util.Random;
public class Main {
public static void main(String[] args) {
//创建大集合
ArrayList<Integer> list = new ArrayList<>();
//创建小集合
ArrayList<Integer> list1 = new ArrayList<>();
//产生随机数并且将其存入集合
Random r = new Random();
for (int i = 0; i < 20; i++) {
int num = r.nextInt();//没有要求随机数的范围,随机数为空
list.add(num);//将随机数放入大集合
if(num % 2 == 0){//判断:当随机数是偶数
list1.add(num);
}
}
//分别输出大集合、小集合
System.out.println(list);
System.out.println(list1);
}
}
输出结果:
[-453027090, 1664512285, -1125799748, 423028440, 773751461, -561310230, -2020010969, -319309811, -577037958, -755408249, -344171246, -42682123, -1706010920, -240963106, 871160400, 1533630221, -1290753801, -358166779, 870940833, 45396495]
=============
[-453027090, -1125799748, 423028440, -561310230, -577037958, -344171246, -1706010920, -240963106, 871160400]