-1

Please tell my what ... means in "doInBackground(X...x)"

I have read the excellent answer by Kartik Domadiya on

What arguments are passed into AsyncTask?

and It helps my a lot. However, I still cannot understand the 3 dots in the following. I need some concrete examples.

private class MyTask extends AsyncTask<X, Y, Z>

protected void onPreExecute(){

}
 protected Z doInBackground(X...x){

}

protected void onProgressUpdate(Y y){
}

protected void onPostExecute(Z z){
}

If I have to pass more than 1 parameters to doInBackground(), will I just replace X by the type of the first parameter?

For instance, if I will pass three parameters to doInBackground(), with types of int, String, String, respectively , and make a result with a type of String. During the progress update, I will use a String type parameter, will I implement the class like that?

private class MyTask extends AsyncTask<int, String, String> {
    protect void onPreExecute() {
    }
    protect String doInBackground(int ... x) {
    }
    protect void onProgressUpdate(String y) {
    }
    protect void onPostExecute(String z) {
    }
Eric Andrews
  • 936
  • 8
  • 10
  • 1
    Related canonical question: [When do you use varags in Java](https://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java)? – David Rawson Aug 22 '17 at 03:58

3 Answers3

0

According to docs:

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result. These three types can be explained as:

The three types used by an asynchronous task are the following:

1.Params- the type of the parameters sent to the task upon execution.

2.Progress- the type of the progress units published during the background computation.

3.Result, the type of the result of the background computation.

You can get further details here.

Now; X is generally an URL that is sent to the task upon execution.

Now; The ... (dots) implies the array of variable arguments i.e. A variable number of arguments of the same type can be passed to a method and treated as an array.

For instance;

the three dot stays for vargars. you can access it like a String[].

If a method takes as parameter a varargs, you can call it with multiple values for the vargars type:

public void myMethod(String... values) {}
you can call like myMethod("a", "b");

in myMethod values[0] is equals "a" and values[1] is equals to "b". If you have a method with multiple args, the vargars argument has to be the last: for instance:

 public void myMethod(int first, double second, String... values) {}
  • I just do not under stand the 3 dots in doInBackground(X...x), can you tell me what ... means? – Eric Andrews Aug 22 '17 at 03:43
  • I think it should clarify your doubts –  Aug 22 '17 at 03:52
  • Thank you very muck . By the way, if I have to pass an integer array of parameters, will I replace X by Integer or just int? – Eric Andrews Aug 22 '17 at 03:56
  • Of Course; you can just use something like `Integer ...x` –  Aug 22 '17 at 03:58
  • I now understand it quit well, with your help. I am just pity that I just have 6 reputations now and cannot increase your reputations. Thank you again. – Eric Andrews Aug 22 '17 at 04:04
  • If it was helpful, you can click on the accept answer sign(The green tick mark) to help me and don't feel pity because everyone was once a beginner –  Aug 22 '17 at 04:13
0

It means you can pass an undeterminate number of parameters of that type. It is a shorthand for X [] x. Which means the parameter is actually an array of X.

Example:

public void doSomething(String ... stings){
  String [] str = strings;
  <process the stings>
}

somewhere else:

doSomething("One","of","many","stirngs");
Juan
  • 5,525
  • 2
  • 15
  • 26
  • Thanks a lot, that means I can pass an array of data with same type, right? But how can I pass parameters with different types. Will I had to wrap these parameter into an object? – Eric Andrews Aug 22 '17 at 03:50
  • But you pass it as a coma separated list of parameters. – Juan Aug 22 '17 at 03:50
  • When you write the function you read the parameters as an array of that type. – Juan Aug 22 '17 at 03:51
  • I now understand it quit well, with your help. I am just pity that I just have 6 reputations now and cannot increase your reputations. Thank you again. – Eric Andrews Aug 22 '17 at 04:04
  • If this helped you, you should be able to accept the answer with the green tick :). – Juan Aug 22 '17 at 04:11
  • I am sorry, I just get in and I have only 6 reputations. The system only allow those to click the green tick, with reputations more than 15. :( – Eric Andrews Aug 22 '17 at 04:21
0

It means that the argumetns could be zero or more Int objects (or an array of them) for that method.

You can have more details here:

"Arbitrary Number of Arguments" section http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html#varargs

So you could do

new MyTask().execute();
new MyTask().execute(1);
new MyTask().execute(1, 2, 3);
rookiedev
  • 1,057
  • 2
  • 9
  • 21
  • I was programming in JavaScript and ruby, and was not familiar with Java. I will read varargs for more detail, thank you. – Eric Andrews Aug 22 '17 at 04:06