Is it possible to pass multiple numbers as a parameter for an array parameter? Just like I would with a regular int. Or does the parameter have to be a separate array that I have to create?
是否可以将多个数字作为数组参数的参数传递?就像我对常规int一样。或者参数必须是我必须创建的单独数组?
public static void main(String[] args) {
public static void main(String [] args){
getIntegers(1,2,3,4,5);
}
public static void getIntegers(int[] array) {
public static void getIntegers(int [] array){
//write whatever here
//在这里写点什么
}
1 个解决方案
#1
1
You can use varargs.
你可以使用varargs。
public static void getIntegers(int... array)
It can be referenced as an int[]
from within the method body.
它可以从方法体中引用为int []。
The method can be invoked by passing any given number of int
s, or null
.
可以通过传递任意给定数量的int来调用该方法,或者为null。
Also note
- You cannot declare more than one varargs per method signature.
- If you intend to declare more than one parameter in your method, the varargs must be the last one
您不能为每个方法签名声明多个varargs。
如果您打算在方法中声明多个参数,则varargs必须是最后一个
#1
1
You can use varargs.
你可以使用varargs。
public static void getIntegers(int... array)
It can be referenced as an int[]
from within the method body.
它可以从方法体中引用为int []。
The method can be invoked by passing any given number of int
s, or null
.
可以通过传递任意给定数量的int来调用该方法,或者为null。
Also note
- You cannot declare more than one varargs per method signature.
- If you intend to declare more than one parameter in your method, the varargs must be the last one
您不能为每个方法签名声明多个varargs。
如果您打算在方法中声明多个参数,则varargs必须是最后一个