views:

152

answers:

5

Possible Duplicate:
What is the ellipsis for in this method signature?

I saw "T... elements" in java code. what does that mean? thanks!

+5  A: 

It means, you can pass all T variables you want to a method.

If a method is:

public void myMethod(String... a)
{

}

You can make call with all String objects you want to that method:

myMethod("s1");
myMethod("s1", "s2");
myMethod("s1", "s2", "s3");
myMethod("s1", "s2", "s3", "s4");

Here's Java official documentation on that language feature. It was introduced in Java 5.

Pablo Santa Cruz
+5  A: 

That is the 'var args' specifier.

It means that you can pass in 0 or more elements into the method.

You would use it like this:

public void method(String str, int... elements) {
    for (int i = 0; i < elements.length; i++) {
        print(str + " " + elements[i];
    }
}

and, you can call it like this:

method("Hi");
method("He", 1, 2 , 3, 4, 5);
method("Ha", 1 , 2);
int[] arr = {1, 2 , 3 ,3 ,4};
method("Ho", arr);
jjnguy
+2  A: 

It is a placeholder for an array of T (T[]) where the array itself can be specified as t1,t2,t3 etc. where all the ts are of type T.

CF: http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

Andreas
+2  A: 

It is used in method declaration:

void someMethod(String... strings) {
  //blabla method body
}

means you can put array of Strings as parameter, or Strings separated by comma. Both calls are valid:

someMethod("a", "b", "c");

String[] stringsArray = new String[] {"a", "b", "c"};
some Method(stringsArray);
amorfis
+1  A: 

This means that you can pass in 0 or more instances of object T. You can access those parameters as an array.

Example:

public void printStrings(String... strings)
{
    for (String string : strings) {
        System.out.println(string);
    }
}

You can then call this method using any of these calls:

printStrings("one", "two", "three", "four");
printStrings(new String[] { "one", "two", "three", "four" } );
printStrings(/* any array of strings, even an empty array */);

In your case. T... means you can pass in any number of instances of type T, which is a generic that should be defined at the top of your class file.

David