Java 实例 - 查找 List 中的最大最小值

3358 1
LZH实名认证 2022-4-9 17:01:12 | 显示全部楼层 |阅读模式
以下实例演示了如何使用 Collections 类的 max() 和 min() 方法来获取List中最大最小值:

Main.java 文件
import java.util.*;

public class Main {
   public static void main(String[] args) {
      List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
      System.out.println(list);
      System.out.println("最大值: " + Collections.max(list));
      System.out.println("最小值: " + Collections.min(list));
   }
}
以上代码运行输出结果为:

[one, Two, three, Four, five, six, one, three, Four]
最大值: three
最小值: Four
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行