我想将数组转换为ArrayList或任何集合,并从该集合中获取最大大小。

huangapple go评论55阅读模式
英文:

First I want to convert array to Arraylist or any collection and to get the max size from that collection

问题

Solution.java:17: 错误:不兼容的类型
    Integer max = Collections.max(list);
                                  ^
需要类型:Integer
找到类型:Object
英文:
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

答案1

得分: 2

首先,您不能在对象列表上使用Collections.max,在这里Arrays.asList将int数组转换为List<int[]>而不是List<int>

您可以使用Arrays.streammax()来获取最大值。

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

您可以首先将其转换为列表。

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

然后获取最大值。

Integer max = Collections.max(list);
英文:

First you can not use Collections.max for List of Object and here Arrays.asList convert int array into List<int[]> not List<int>

You can use Arrays.stream and max() to get max value

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

You can first convert into a list

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

Then get max

  Integer max= Collections.max(list);

huangapple
  • 本文由 发表于 2020年7月30日 19:31:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63172282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定