集册 Java实例教程 收集方法反转,填充,复制,最大和最小。

收集方法反转,填充,复制,最大和最小。

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

509
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
集合方法reverse、fill、copy、max和min。

import java.util.List;

import java.util.Arrays;// 来 自 NowJava.com - 时  代  Java

import java.util.Collections;


public class Main 

{

   public static void main(String[] args)

   {

      // create and display a List< Character >

      Character[] letters = {'P', 'C', 'M'};

      List<Character> list = Arrays.asList(letters); // get List

      System.out.println("list contains: ");

      output(list);


      // reverse and display the List<Character>/* from N o w J a v a . c o m*/

      Collections.reverse(list); // reverse order the elements

      System.out.printf("%nAfter calling reverse, list contains:%n");

      output(list);


      // create copyList from an array of 3 Characters

      Character[] lettersCopy = new Character[3]; 

      List<Character> copyList = Arrays.asList(lettersCopy); 


      // copy the contents of list into copyList

      Collections.copy(copyList, list);

      System.out.printf("%nAfter copying, copyList contains:%n");

      output(copyList);


      // fill list with Rs 

      Collections.fill(list, 'R'); 

      System.out.printf("%nAfter calling fill, list contains:%n");

      output(list);

   } 


   // output List information

   private static void output(List<Character> listRef)

   {

      System.out.print(
展开阅读全文