Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "String..." in java? [duplicate]

Possible Duplicate:
varargs and the '…' argument
Java, 3 dots in parameters

I saw this definition in my android java file. It looks just like String[]. Are they different? Thank you.

like image 537
AnothrWu Avatar asked Mar 15 '12 10:03

AnothrWu


People also ask

What is duplicate string in Java?

The duplicate characters in a string are those that occur more than once. These characters can be found using a nested for loop. An example of this is given as follows − String = Apple. In the above string, p is a duplicate character as it occurs more than once.

What is duplicate string?

To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.

Which function duplicates a string?

The C library function to copy a string is strcpy(), which (I'm guessing) stands for string copy. Here's the format: char * strcpy(char * dst, const char * src);

How do you find duplicate characters in a string in Java?

To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.


4 Answers

varags. If a method signature is method(Param param, String... x) it will take one Param type of object and any number of String objects.

There are couple if cool things about it:

  1. It's nothing special. It's just sugared array. So, method(MyObject... o) is same as method(MyObject[] o).

  2. Vararg has to be the last parameter in argument list.

  3. There is this funny thing that bit me once. method(MyObject... o) can be called as method() without any compilation error. Java will internally convert the no-arg call to method(new MyObject[0]). So, be aware of this.

like image 184
Nishant Avatar answered Oct 19 '22 20:10

Nishant


It's syntax for writing the items of the array as a parameter

for instance:

 public String first (String... values) {
     return values[0];
 }

Then you can call this method with first("4","2","5","67")

The javacompiler will create an array of the parameters on its own.

like image 30
Willem Van Onsem Avatar answered Oct 19 '22 19:10

Willem Van Onsem


It's a vararg, variable number of arguments. In the method body you treat it as a String[], but when you call the method you can either chose to supply a String[] or simply enumerate your values.

void foo(String... blah) { }

void bar() {
   String[] a = { "hello", "world" };
   foo(a);  // call with String[]
   foo("hello", "world"); // or simply enumerate items
}

Was introduced with Java 5.

like image 31
user1252434 Avatar answered Oct 19 '22 19:10

user1252434


It's for defining a method with a variable number of arguments.

like image 22
alexg Avatar answered Oct 19 '22 20:10

alexg