0

When defining an ASyncTask and it associated methods in Android there are 3 dots that appear, eg protected Long doInBackground(URL... urls)

What do the dots mean?

4
  • 1
    That it's an array, ... instead of []
    – Bill Gary
    Commented Feb 12, 2012 at 23:45
  • so int[] numbers and int... numbers means the same thing? Commented Feb 12, 2012 at 23:46
  • yup, I'm not sure why AsyncTask does it that way though.
    – Bill Gary
    Commented Feb 12, 2012 at 23:48
  • "..." is not the same as "[]", they somehow refers to the same things but with different usage. And this is not AsyncTask specific, but many classes and methods use this also. "..." in the parameter allows the caller to put indefinite number of parameter, while for "[]", you have to create an array with definite size in advance. E.g. someMethod(String... param) => when calling, you can do this: someMethod(p1, p2, p3, p4) or simply someMethod(p1). Commented Feb 13, 2012 at 1:24

3 Answers 3

3

It's not entirely the same thing. Consider the following examples

Example 1:

public String concatenateStrings(String... strings){
  StringBuffer sb = new StringBuffer();
  for( int i = 0; i < strings.length; i++ )
    sb.append( strings[i] );

  return sb.toString();
}

Example 2:

public String concatenateStrings2(String[] strings){
  StringBuffer sb = new StringBuffer();
  for( int i = 0; i < strings.length; i++ )
    sb.append( strings[i] );

  return sb.toString();
}

They're allmost identical, right? Wrong, calling them is the big difference. The first example allows for an undefined number of strings to be added.

Example 1:

concantenateStrings("hello", "World", " I ", " can ", " add ", " so ", " many strings here" );

Example 2:

Strings[] myStrings = new Strings[7];
myStrings[0] = "Hello";
myStrings[1] = "world";
myStrings[2] = " I ";
...
myStrings[6] = " many strings here";
concatenateStrings2( myStrings );
0

This is not AsynTask specific or Android specific for that matter. It's a Java feature to pass variable length of parameters to a method.

Have a look at: How to create Java method that accepts variable number of arguments?

0

It java concept. It looks like array. (and you process it mostly like you process on array). But, it 's different in some points.

You will meet this commonly in android, for example when you use view animation or property animation for layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.