提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
集合方法addAll、frequency和disjoint。
import java.util.ArrayList;/*NowJava.com - 时代Java*/ import java.util.List; import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { // initialize list1 and list2 String[] colors = {"red", "white", "yellow", "blue"}; List<String> list1 = Arrays.asList(colors); ArrayList<String> list2 = new ArrayList<>(); /** 时 代 Java - nowjava.com 提供 **/ list2.add("black"); // add "black" to the end of list2 list2.add("red"); // add "red" to the end of list2 list2.add("green"); // add "green" to the end of list2 list2.add("red"); // add "red" to the end of list2 list2.add("red"); // add "red" to the end of list2 System.out.print("Before addAll, list2 contains: "); // display elements in vector for (String s : list2) System.out.printf("%s ", s); Collections.addAll(list2, colors); // add colors Strings to list2 System.out.printf("%nAfter addAll, list2 contains: "); for (String s : list2) System.out.printf("%s ", s); int frequency = Collections.frequency(list2, "red"); System.out.printf(