Can I pass an array as arguments to a method with variable arguments in Java?
The underlying type of a variadic method function(Object... args)
is function(Object[] args)
. Sun added varargs in this manner to preserve backwards compatibility.
So you should just be able to prepend extraVar
to args
and call String.format(format, args)
.
Java Pass Array of Arguments
You can do this with reflection, but it's probably more performant to use cached method handles that you initialize once, then reuse as many times as you need.
class SpreadInvoker {
public static void draw1(int x, String y) {
System.out.printf("draw1(%s, %s)%n", x, y);
}
public void draw2(int x, int y) {
System.out.printf("draw2(%s, %s)%n", x, y);
}
static MethodHandle DRAW1;
static MethodHandle DRAW2;
public static void main(String[] args) throws Throwable {
DRAW1 = MethodHandles.lookup()
.findStatic(
SpreadInvoker.class,
"draw1",
MethodType.methodType(
void.class,
int.class,
String.class
)
)
.asSpreader(Object[].class, 2);
DRAW2 = MethodHandles.lookup()
.findVirtual(
SpreadInvoker.class,
"draw2",
MethodType.methodType(
void.class,
int.class,
int.class
)
).asSpreader(Object[].class, 2);
SpreadInvoker instance = new SpreadInvoker();
final Object[] args1 = { 13, "twenty-six" };
final Object[] args2 = { 13, 26 };
DRAW1.invoke(args1); // SpreadInvoker.draw1(13, "twenty-six")
DRAW2.invoke(instance, args2); // instance.draw2(13, 26)
}
}
Output:
draw1(13, twenty-six)
draw2(13, 26)
Note, however, that this is the sort of thing you'd do if you don't know what method you need to call at compile time. This sort of thing should almost never be necessary.
How to pass an array of primitives as varargs?
The String.format(String format, Object... args)
is waiting an Object
varargs as parameter. Since int
is a primitive, while Integer
is a java Object
, you should indeed convert your int[]
to an Integer[]
.
To do it, you can use nedmund answer if you are on Java 7 or, with Java 8, you can one line it:
Integer[] what = Arrays.stream( data ).boxed().toArray( Integer[]::new );
or, if you don't need to have an Integer[]
, if an Object[]
is enough for your need, you can use:
Object[] what = Arrays.stream( data ).boxed().toArray();
Pass multiple args to a method with varargs of Object type
You have a non-variadic parameter head
.
This means that you require at least head
to be passed to the method, with optional variadic arguments after.
Therefore, you should call:
Object[] allArgs = new Object[]{object1, object2, object3};
Object[] varags = Arrays.copyOfRange(allArgs, 1, allArgs.length);
process(allArgs[0], varags)
How to pass an arbitrary number of the same type arguments to a method using varargs?
It's possible to pass an arbitrary number of the same type of arguments to a method using the special syntax named varargs (variable-length arguments). These arguments are specified by three dots after the type. In the body of the method, you can process this parameter as a regular array of the specified type.
Your method takes an integer vararg parameter and outputs the number of arguments in the standard output using the length property of arrays.
...
is a special syntax used here to specify a vararg parameter.
i am trying to provide you both types or passing varargs in methods in java:
incorrect example:
public static void method(double... varargs, int a) { /* do something */ }
The correct version of the method is:
public static void method(int a, double... varargs) { /* do something */ }
Related Topics
How to Compare Two Double Values in Java
Maven Dependencies Are Failing With a 501 Error
Android Custom Row Item for Listview
How Delete a Collection or Subcollection from Firestore
How to Delete Firebase Data After "N" Days
Find Directory for "Application Data" on Linux and MACintosh
Java App Behind Proxy to Use Http_Proxy Variable in Linux
Which Operating Systems Support Native (Inotify-Like) File Watching in Java
Java.Lang.Unsatisfiedlinkerror in Linux
How to Add Multiple Jar Files in Classpath in Linux
Why Do I Get "Exception; Must Be Caught or Declared to Be Thrown" When I Try to Compile My Java Code
Split String With Dot as Delimiter
How to Connect Remote MySQL Database in Android Using Jdbc
How to Programmatically Set Drawableleft on Android Button
Division in Java Always Results in Zero (0)
Why Does Reading from Process' Inputstream Block Altough Data Is Available