[英]Why this java Class doesn't compile
Why does the below java code compilation result in an error? 为什么下面的Java代码编译会导致错误? I'm using java 8, if that matters. 我正在使用Java 8,如果那很重要的话。
public class SimpleTest {
private static boolean isPresent(int []... arrays, int number){
boolean isPresent = true;
for(int i=0;i<arrays.length;i++){
//isPresent = doBinarySearch(arrays[i], number);
if(!isPresent){
break;
}
}
return isPresent;
}
}
And here is the error I am getting: 这是我得到的错误:
SimpleTest.java:3: error: ')' expected
private static boolean isPresent(int []... arrays, String number){
^
SimpleTest.java:3: error: ';' expected
private static boolean isPresent(int []... arrays, String number){
^
2 errors
Varargs can only be declared as the last parameter. 可变参数只能声明为最后一个参数。
You can use a construct called varargs to pass an arbitrary number of values to a method. 您可以使用称为varargs的构造将任意数量的值传递给方法。 You use varargs when you don't know how many of a particular type of argument will be passed to the method. 当您不知道将多少种特定类型的参数传递给方法时,可以使用varargs。 It's a shortcut to creating an array manually. 这是手动创建数组的快捷方式。
To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name. 要使用varargs,请在最后一个参数的类型后加上省略号(三个点,...),然后是空格和参数名称。 The method can then be called with any number of that parameter, including none. 然后可以使用任意数量的该参数(包括无)调用该方法。
This should work : 这应该工作:
private static boolean isPresent(int []arrays, int number) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.