What is the "..." in java?

Possible duplicate:
What is ellipsis in this method signature?

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

+4
source share
5 answers

This means that you can pass all the T variables that you want to use.

If the method:

public void myMethod(String... a) { } 

You can make a call with all String objects that you want to use for this method:

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

Here is the official Java language documentation. It was introduced in Java 5 .

+7
source

This is the 'var args' specifier .

This means that you can pass 0 or more elements to a method.

You would use it as follows:

 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); 
+7
source

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

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

+2
source

It is used in a method declaration:

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

means you can put an array of strings as a parameter or comma separated strings. Both calls are valid:

 someMethod("a", "b", "c"); String[] stringsArray = new String[] {"a", "b", "c"}; some Method(stringsArray); 
+2
source

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

Example:

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

You can 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 that you can pass any number of instances of type T, which is shared, which should be defined at the top of your class file.

+1
source

All Articles