What do the String... and Void... keywords mean?
What is the difference between String
and String...
and what is the difference between Void
and Void...
in this context?
class AddStringsTask extends AsyncTask<Void, String, Void>{
@Override
protected Void doInBackground(Void... unused) {
开发者_Go百科 for (String item: items){
publishProgress(item);
SystemClock.sleep(200);
}
return(null);
}
@Override
protected void onProgressUpdate(String... item){
((ArrayAdapter)getListAdapter()).add(item[0]);
}
@Override
protected void onPostExecute(Void unused){
Toast.makeText(Cap15Asyncer.this,"Completed!" , Toast.LENGTH_SHORT).show();
}
}
It means that there might be a variable number of String
parameters in the function call, it is called varargs.
In the method
protected void onProgressUpdate(String... item){
((ArrayAdapter)getListAdapter()).add(item[0]);
}
String... item
Means that the method takes an arbitrary number of Strings (including 0 Strings). So this method can be called with any number of strings as input, and it will add them all to the listAdapter, like an array. If the method was:
protected void onProgressUpdate(String item){
((ArrayAdapter)getListAdapter()).add(item);
}
Then it would take exactly one String, (note the add method has changed accordingly).
As you are mentioning void
, I would assume that you are asking about the return type on a method. void
means that there will be no value returning when called and string
means that a type of string will be returning from your method.
Return types explicitly tell the calling object what kind of data to not only expect to get back, but to guarentee.
Taking into account your code
kprotected Void doInBackground(Void... unused) {
for (String item: items){
publishProgress(item);
SystemClock.sleep(200);
}
Void
means that there will be no return
This would however return something
kprotected something doInBackground(<your parameters here>){
//do your work
return something
}
String is something that is stored as text.
Your case each item that is stored as a string
in items.(the bigger picture) will have work done on it.
EDIT:
When passing in multiple arguements as in the String array using String...
automates the process of multiple arguements being used. Notice how it is not used in the last method.
And yes as mentioned in another answer this is Varargs
精彩评论