Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Collections.max(数组)不工作-Java_Java_Arrays - Fatal编程技术网

Collections.max(数组)不工作-Java

Collections.max(数组)不工作-Java,java,arrays,Java,Arrays,我正在使用Collections.max(数组);以确定所选阵列中的最大数字。但是,当我使用它时,会收到错误消息: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method max(Collection<? extends T>) in the type Collections is not applicable for the arguments (int)

我正在使用Collections.max(数组);以确定所选阵列中的最大数字。但是,当我使用它时,会收到错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method max(Collection<? extends T>) in the type Collections is not applicable for the arguments (int)

    at Array_6.main(Array_6.java:23)
线程“main”java.lang中出现异常。错误:未解决的编译问题:
方法max(Collection
Collections
是Collections API的一个实用类。
arrayInt
是一个
int
数组,它与
Collections
类提供的任何内容都完全不兼容

你可以试着用

highestNumber = Collections.max(Arrays.asList(intArray));
它将
intArray
包装在
列表
界面中,使其与
集合
API兼容

正如@LouisWasserman所指出的,上述方法不起作用,因为
Collections.max
需要一个
对象
而不是基于原语的
Collection

最简单的解决方案可能是在输入值时检查每个值,例如

for (int i = 0; i < 10; i++) {
    System.out.print(i + 1 + ": ");
    input = TextIO.getlnInt();  // Retrieves input
    intArray[i] = input;
    highestNumber = Math.max(input, highestNumber);
}
for(int i=0;i<10;i++){
系统输出打印(i+1+“:”);
input=TextIO.getlnInt();//检索输入
intArray[i]=输入;
highestNumber=Math.max(输入,highestNumber);
}
或者您也可以使用
Arrays.sort(intArray)
intArray
进行排序,只需选取数组中的最后一个元素,它将是最高的

或者您可以创建第二个循环,并使用
Math.max
遍历
intArray
中的每个元素,以找到最大值

或者您可以创建一个
列表
集合,并将
intArray
中的每个项目添加到其中,然后使用
Collections.max

或者您可以使用
Integer[]
数组而不是
int[]
数组


或者您可以创建一个
列表
集合,而不是完全使用数组…

集合
是集合API的一个实用类。
arrayInt
是一个
int
数组,它与
集合
类提供的任何内容都完全不兼容

你可以试着用

highestNumber = Collections.max(Arrays.asList(intArray));
它将
intArray
包装在
列表
界面中,使其与
集合
API兼容

正如@LouisWasserman所指出的,上述方法不起作用,因为
Collections.max
需要一个
对象
而不是基于原语的
Collection

最简单的解决方案可能是在输入值时检查每个值,例如

for (int i = 0; i < 10; i++) {
    System.out.print(i + 1 + ": ");
    input = TextIO.getlnInt();  // Retrieves input
    intArray[i] = input;
    highestNumber = Math.max(input, highestNumber);
}
for(int i=0;i<10;i++){
系统输出打印(i+1+“:”);
input=TextIO.getlnInt();//检索输入
intArray[i]=输入;
highestNumber=Math.max(输入,highestNumber);
}
或者您也可以使用
Arrays.sort(intArray)
intArray
进行排序,只需选取数组中的最后一个元素,它将是最高的

或者您可以创建第二个循环,并使用
Math.max
遍历
intArray
中的每个元素,以找到最大值

或者您可以创建一个
列表
集合,并将
intArray
中的每个项目添加到其中,然后使用
Collections.max

或者您可以使用
Integer[]
数组而不是
int[]
数组

或者您可以创建一个
列表
集合,而不是完全使用数组…

在Java 8中

highestNumber = Arrays.stream(intArray).max().getAsInt();
在Java 8中

highestNumber = Arrays.stream(intArray).max().getAsInt();

我输入的代码不正确,但错误仍然是正确的same@Jordan.McBride请参阅更新(另外,这是一个很好的理由,为什么有一个准确的示例会有很大帮助。)@MadProgrammer:
数组。asList
的工作方式与
int[]的工作方式不同。它返回一个
列表
,而不是
列表
。我不相信JDK中有一行解决方案可以解决这个问题。为什么不使用List而不是int[]作为开头呢?那么这个转换就没有必要了。@Leo这是“最简单”解决方案下的第一个选项,或者您也可以使用
数组。sort(intArray)
“我输入的代码不正确,但错误仍然是唯一的same@Jordan.McBride请参阅更新(另外,这也是一个很好的理由,说明有一个准确的示例会有很大帮助;))@MadProgrammer:
Arrays.asList
的工作方式与
int[]
的工作方式不同。它返回一个
列表
,而不是
列表
。我不相信JDK中有一行解决方案可以解决这个问题。为什么不使用List而不是int[]作为开头呢?这样,这种转换就没有必要了。@Leo这是“最简单”解决方案下的第一个选项,或者您也可以使用
Arrays.sort(intArray)