首页 > 解决方案 > 首先,我想将数组转换为 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

标签: java

解决方案


首先,您不能Collections.max用于 List of Object,在这里Arrays.asList将 int 数组转换为List<int[]> notList<int>

您可以使用Arrays.streamandmax()来获得最大值

int max = Arrays.stream(height).max().getAsInt();

您可以先转换为列表

List<Integer> list = Arrays.stream(height).boxed().collect(Collectors.toList());

然后得到最大值

  Integer max= Collections.max(list);

推荐阅读