打印1-100以內的20個隨機數大集合中的偶數java
package cn.itcast.day04.demo03; import java.util.ArrayList; import java.util.Random; /** * @author ChenDan * @date 2019/7/9 13:19 */ public class Demo01SelectArray { public static void main(String[] args) { ArrayList<Integer> big = new ArrayList<>(); Random r = new Random(); for (int i = 0; i < 20; i++) { int num = r.nextInt(100) + 1; big.add(num); } ArrayList<Integer> small = getSmall(big); System.out.println(small.size()); for (int i = 0; i < small.size(); i++) { System.out.println(small.get(i)); } } public static ArrayList<Integer> getSmall(ArrayList<Integer> big) { ArrayList<Integer> small = new ArrayList<>(); for (int i = 0; i < big.size(); i++) { int num = big.get(i); if (num % 2 == 0) { small.add(num); } } return small; } }