Important Points
- you have to use java.util package
- array can be passed by reference
In the method calling statement
- Don't use any object to pass an array
- only the array's name is used, don't use datatype or array brackets []
Sample Program
import java.util.*; class atg < void a() < int b[]=; c(b); > void c(int b[]) < int e=b.length; for(int f=0;f> public static void main(String args[]) < atg ob=new atg(); ob.a(); >>
Источник
Can I pass an array as arguments to a method with variable arguments in Java?
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.
I can't help but wonder why this question is a "is this valid" kind of question. Couldn't you have just tried it? Don't overdo asking, you'll do yourself more harm than good.
true enough, this could have been easily tested. however, the nice thing about a question like this is that it exposes the topic and solicits interesting answers.
I actually tried the code above, with the intention to pass the array args as arguments to the method. However, I didn't realize that I should've prepended extraVar to args first. When you know that variable arguments are treated as an array (even outside the method), this is of course quite logical.
6 Answers 6
Yes, a T. is only a syntactic sugar for a T[] .
The last formal parameter in a list is special; it may be a variable arity parameter, indicated by an elipsis following the type.
If the last formal parameter is a variable arity parameter of type T , it is considered to define a formal parameter of type T[] . The method is then a variable arity method. Otherwise, it is a fixed arity method. Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation.
Here's an example to illustrate:
public static String ezFormat(Object. args) < String format = new String(new char[args.length]) .replace("\0", "[ %s ]"); return String.format(format, args); >public static void main(String. args) < System.out.println(ezFormat("A", "B", "C")); // prints "[ A ][ B ][ C ]" >
And yes, the above main method is valid, because again, String. is just String[] . Also, because arrays are covariant, a String[] is an Object[] , so you can also call ezFormat(args) either way.
See also
Varargs gotchas #1: passing null
How varargs are resolved is quite complicated, and sometimes it does things that may surprise you.
static void count(Object. objs) < System.out.println(objs.length); >count(null, null, null); // prints "3" count(null, null); // prints "2" count(null); // throws java.lang.NullPointerException.
Due to how varargs are resolved, the last statement invokes with objs = null , which of course would cause NullPointerException with objs.length . If you want to give one null argument to a varargs parameter, you can do either of the following:
count(new Object[] < null >); // prints "1" count((Object) null); // prints "1"
The following is a sample of some of the questions people have asked when dealing with varargs:
As you've found out, the following doesn't "work":
String[] myArgs = < "A", "B", "C" >; System.out.println(ezFormat(myArgs, "Z")); // prints "[ [Ljava.lang.String;@13c5982 ][ Z ]"
Because of the way varargs work, ezFormat actually gets 2 arguments, the first being a String[] , the second being a String . If you're passing an array to varargs, and you want its elements to be recognized as individual arguments, and you also need to add an extra argument, then you have no choice but to create another array that accommodates the extra element.
Here are some useful helper methods:
static T[] append(T[] arr, T lastElement) < final int N = arr.length; arr = java.util.Arrays.copyOf(arr, N+1); arr[N] = lastElement; return arr; >static T[] prepend(T[] arr, T firstElement)
Now you can do the following:
String[] myArgs = < "A", "B", "C" >; System.out.println(ezFormat(append(myArgs, "Z"))); // prints "[ A ][ B ][ C ][ Z ]" System.out.println(ezFormat(prepend(myArgs, "Z"))); // prints "[ Z ][ A ][ B ][ C ]"
Varargs gotchas #3: passing an array of primitives
int[] myNumbers = < 1, 2, 3 >; System.out.println(ezFormat(myNumbers)); // prints "[ [I@13c5982 ]"
Varargs only works with reference types. Autoboxing does not apply to array of primitives. The following works:
Integer[] myNumbers = < 1, 2, 3 >; System.out.println(ezFormat(myNumbers)); // prints "[ 1 ][ 2 ][ 3 ]"
Источник
How does the Java array argument declaration syntax ". " work?
Note the "dot dot dot" in the array declaration, rather than the usual bracket []. Clearly it works. In fact I wrote a small test and verified it works. So, I pulled the java grammar to see where this syntax of argument declaration is, but did not find anything. So to the experts out there, how does this work? Is it part of the grammar? Also, while I can declare function like this, I can't declare an array within a function's body like this. Anyway, do you know of any place that has this documented. It is curiosity, and perhaps not worth of any time invested in it, but I was stumped.
10 Answers 10
I believe this was implemented in Java 1.5. The syntax allows you to call a method with a comma separated list of arguments instead of an array.
public static void main(String. args); main("this", "is", "multiple", "strings");
public static void main(String[] args); main(new String[] );
If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]. The method is then a variable arity method. Otherwise, it is a fixed arity method. Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation (§15.12.4.2).
Basically, the last parameter of any method call can have T. . If it has that, it is converted to T[] .
So basically, what you have is a fancy way of reproducing the more traditional
In simple term its an Array of Member like
public setMembers(Member[] members);
When to use:
Generally while designing API it is good to use when number of argument is not fixed.
Источник
How to call a "vararg" method with an array of params?
. assuming you want the args to be the multiple values. If you need extra wrapping, you have to do that yourself:
String text = String.format(formatString, new Object[] < args >);
Basically, if the argument type already matches the parameter array type, the compiler doesn't do any wrapping.
If m is being invoked with kn actual argument expressions, or, if m is being invoked with k=n actual argument expressions and the type of the kth argument expression is not assignment compatible with T[], then the argument list (e1, . , en-1, en, . ek) is evaluated as if it were written as (e1, . en-1, new T[]).
Note the "If" at the start - that's what effectively says that if the argument is already okay, no wrapping is performed.
in short by example: (S2 and I2 are likely the most interesting versions passing the varargs as an Object[] )
PrintStream ps = System.out ; String[] sarr = new String[] < "a" , "b" >; Object os = sarr ; int[] iarr = new int[] < 1 , 2 >; // cannot be cast to Object[] - see I6 below Integer[] ioarr = new Integer[] < 1 , 2 >; // can be cast to Object[] - see I2 below Object oi = iarr ; // cannot be cast to Object[] - see I6 below ps.printf( "%n## Strings:%n%n" ) ; ps.printf( "S1: pass as single objects: %s, %s%n" , "a" , "b" ) ; ps.printf( "S2: pass as varargs objects: %s, %s%n" , (Object[]) sarr ) ; ps.printf( "S3: pass as single objects: %s, %s%n" , sarr , "c" ) ; ps.printf( "S4: pass as single objects: %s, %s%n" , os , "c" ) ; // ps.printf( "S5: MissingFormatArgumentException: %s, %s%n" , os ) ; // ps.printf( "S6: ClassCastException: %s, %s%n" , (Object[]) os ) ; ps.printf( "%n## int:%n%n" ) ; ps.printf( "I1: pass as single objects: %s, %s%n" , 1 , 2 ) ; ps.printf( "I2: pass as varargs objects: %s, %s%n" , (Object[]) ioarr ) ; ps.printf( "I3: pass as single objects: %s, %s%n" , iarr , 3 ) ; ps.printf( "I4: pass as single objects: %s, %s%n" , oi , 3 ) ; // ps.printf( "I5: MissingFormatArgumentException %s, %s%n" , iarr ) ; // ps.printf( "I6: ClassCastException: %s, %s%n" , (Object[]) oi ) ;
## Strings: S1: pass as single objects: a, b S2: pass as varargs objects: a, b S3: pass as single objects: [Ljava.lang.String;@ad02649, c S4: pass as single objects: [Ljava.lang.String;@ad02649, c ## int: I1: pass as single objects: 1, 2 I2: pass as varargs objects: 1, 2 I3: pass as single objects: [I@6781882a, 3 I4: pass as single objects: [I@6781882a, 3
Источник