This should be a fairly simple question to answer. I looked around and couldn’t find any topics on this syntax, and the “…” makes searching for it difficult on Google. I’m working on a simple test application for copying a database file from its protected location on an un-rooted Android phone to a place on the SD Card that I can access for viewing with the sqlite3 database viewing tool. I know this seems like a roundabout way of doing things, but the emulator refuses to open on my netbook, so I’m using my cell phone to test the development for right now.
The code has already been written, so I’m borrowing it from here and adapting it to my code. I’ve come across this little snippet of code:
private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
private final ProgressDialog dialog = new ProgressDialog(ManageData.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Exporting database...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Boolean doInBackground(final String... args) {
I’ve never seen the argument final String... args before. What does this mean/do?
Thank you!
Moscro
The ellipses in the argument in Java means vararg of the type. So, in your case,
...means you can pass any number of String parameters to the doInBackground methods.So, you can call this method
doInBackground("String1", "String2", "String3")or alsodoInBackground("String1")or alsodoInBackground("String1", "String2", "String3", "String4")or any other form.See here http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
and
finalhas it’s usual meaning. BTW, this feature is available in Java5 or newer.Refer: http://www.developer.com/java/other/article.php/3323661
As the above two links mentions, it’s nothing but sugared array. Basically, when you define
myMethod(T... vararg), Java sees it asmyMethod(T[] vararg). And when you call,myObj.myMethod(t1, t2, t3, ..., tn), Java passes it asmyObj.myMethod(new T[]{t1, t2, t3, ..., tn}).