java - 首先,我想将数组转换为 Arraylist 或任何集合,并从该集合中获取最大大小
问题描述
static int hurdleRace(int k, int[] height) {
List list = Arrays.asList((height));
Integer max=Collections.max(list);
}
Solution.java:17: error: incompatible types
Integer max=Collections.max(list);
^
required: Integer
found: Object
解决方案
首先,您不能Collections.max
用于 List of Object,在这里Arrays.asList
将 int 数组转换为List<int[]>
notList<int>
您可以使用Arrays.stream
andmax()
来获得最大值
int max = Arrays.stream(height).max().getAsInt();
您可以先转换为列表
List<Integer> list = Arrays.stream(height).boxed().collect(Collectors.toList());
然后得到最大值
Integer max= Collections.max(list);
推荐阅读
- javascript - 通量存储变量不在范围内
- python - 使用 axios 进行 Django 表单集验证
- c++ - DirectInput 编译错误 (C++)
- python - 在 django 应用程序中通过移动设备上传时未显示某些图像,但在上传笔记本电脑时始终可以正常工作?
- polymer - 为非根路径构建聚合物应用程序
- mysql - 数据未使用 jsp 插入 mysql db - 在运行 process.jsp 时显示空白页面
- javascript - 选择子菜单后如何使导航栏的主菜单处于活动状态(突出显示)
- python - 如何删除csv中抓取数据之间的空格?
- angular - 角度路由器出口名称不适用于延迟加载
- unity3d - 我不能在 Unity 中的游戏对象前面有 UI 元素