Why does the below java code compilation result in an error? I'm using java 8, if that matters.
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. You use varargs when you don't know how many of a particular type of argument will be passed to the method. 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. The method can then be called with any number of that parameter, including none.
( Source )
This should work :
private static boolean isPresent(int []arrays, int number) {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.