1
4

4 回答 4

4

... is a variable argument list.

Inside the function it may be treated as an array.

于 2013-02-07T14:57:40.837 回答
2

The ... in general stands for variable number of arguments

So you can have a method with variable number of arguments of any type.

The strange symbol you're seeing in your output has nothing to do with this. You may try to see what's inside with Arrays.toString() method:

$ cat A.java 
import java.util.Arrays;


public class A {
    public void example( String ... args ) {
        System.out.println( Arrays.toString( args ) );
    }
    public static void main( String ... args ) {
        A a = new A();
        a.example("ping", "pong\n");
        a.example("That's", "all", "folks");
    }
}

$ java A
[ping, pong
]
[That's, all, folks]
于 2013-02-07T14:56:47.337 回答
2

When you call the function, it turns the list of comma separated arguments into an array of strings.

于 2013-02-07T14:58:42.937 回答
1

String... is indeed a syntaxic sugar that replaces String[] and allows you to pass a undefined numbers of arguments to a method.

Example :

public void withArray(String[] values){ ... }
withArray(new String[] {"a", "b"});

public void withVarArgs(String... values) { ... }
withVarArgs("a", "b");

Variable args allow you to pass arguments directly, skipping new String[] declaration.

于 2013-02-07T15:00:16.960 回答