代码如下:
package com.java.day06; import java.util.ArrayList; import java.util.Random; // 筛选集合中的随机数,得出偶数 public class ArrayListReturn { public static void main(String[] args) { ArrayList<Integer> arrayList = numberSet(20); System.out.println(arrayList); ArrayList<Integer> evenNumber = filterSet(arrayList); System.out.println(evenNumber); } // 产生数字大集合方法 public static ArrayList<Integer> numberSet(int loopNumber) { ArrayList<Integer> numberList = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < loopNumber; i++) { Integer number = random.nextInt(100) + 1; numberList.add(number); } return numberList; } // 过滤数字方法 public static ArrayList<Integer> filterSet(ArrayList<Integer> integerArrayList) { ArrayList<Integer> intArray = new ArrayList<>(); for (int i = 0; i < integerArrayList.size(); i++) { int num = integerArrayList.get(i); if (num % 2 == 0) { intArray.add(num); } } return intArray; } }