Can I pass an array as arguments to a method with variable arguments in Java?
Posted
by user352382
on Stack Overflow
See other posts from Stack Overflow
or by user352382
Published on 2010-05-27T21:36:38Z
Indexed on
2010/05/27
22:01 UTC
Read the original article
Hit count: 156
I'd like to be able to create a function like:
class A {
private String extraVar;
public String myFormat(String format, Object ... args){
return String.format(format, extraVar, args);
}
}
The problem here is that args is treated as Object[] in the method myFormat, and thus is a single argument to String.format, while I'd like every single Object in args to be passed as a new argument. Since String.format is also a method with variable arguments, this should be possible.
If this is not possible, is there a method like String.format(String format, Object[] args)? In that case I could prepend extraVar to args using a new array and pass it to that method.
© Stack Overflow or respective owner