集册 Java实例教程 集合方法addAll,频率和不相交。

集合方法addAll,频率和不相交。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

389
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
集合方法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(                                   

         
展开阅读全文