什么语法"最后的字符串参数... args"意思/怎么办? [英] What does the syntax "final String... args" mean/do?

查看:108
本文介绍了什么语法"最后的字符串参数... args"意思/怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个相当简单的问题来回答。我环顾四周,找不到这个语法的题目,而...使得搜索很难在谷歌。我工作的一个简单的测试应用程序复制从一个未植根Android手机的保护位置的​​数据库文件的SD卡的地方,我可以进入观看的的 sqlite3的数据库查看工具。我知道,这似乎是在做事情的一种迂回的方式,但模拟器拒绝打开我的上网本,所以我用我的手机测试开发的现在。

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.

在code已经被写入,所以我<一个href="http://totsp.com/svn/repo/AndroidExamples/trunk/src/com/totsp/androidexamples/ManageData.java">borrowing从这里并适应我的code。我遇到的code这个小片段:

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) {

最后的字符串参数... args 以前从来没有见过的说法。这是什么意思/怎么办?

I've never seen the argument final String... args before. What does this mean/do?

感谢您! Moscro

Thank you! Moscro

推荐答案

在Java中的参数的省略号意味着可变参数的类型。所以,你的情况, ... 意味着你可以通过任何数量的字符串参数对doInBackground方法。

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.

所以,你可以调用这个方法 doInBackground(字符串1,字符串2,STRING3)或还 doInBackground(字符串1 )或还 doInBackground(字符串1,字符串2,STRING3,串,)或任何其他形式。

So, you can call this method doInBackground("String1", "String2", "String3") or also doInBackground("String1") or also doInBackground("String1", "String2", "String3", "String4") or any other form.

在这里看到<一href="http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html">http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

最后一个参数的类型后三个时期表明,最终的参数,可通过为数组或参数序列。可变参数只能用在最后一个参数的位置。鉴于新的可变参数声明MessageFormat.format,上述调用可以通过以下更短和更甜调用替换

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position. Given the new varargs declaration for MessageFormat.format, the above invocation may be replaced by the following shorter and sweeter invocation

最后都有它的通常含义。顺便说一句,此功能可在Java5中或更高版本。

and final has it's usual meaning. BTW, this feature is available in Java5 or newer.

参见:<一href="http://www.developer.com/java/other/article.php/3323661">http://www.developer.com/java/other/article.php/3323661

由于上述两个环节提到了,这是什么,但糖阵列。基本上,当你定义在myMethod(T ...可变参数),爪哇把它看作在myMethod(T []可变参数)。当你打电话, myObj.myMethod(T1,T2,T3,...,TN),爪哇并将其作为 myObj.myMethod(新T [] {T1,T2,T3,...,TN})

As the above two links mentions, it's nothing but sugared array. Basically, when you define myMethod(T... vararg), Java sees it as myMethod(T[] vararg). And when you call, myObj.myMethod(t1, t2, t3, ..., tn), Java passes it as myObj.myMethod(new T[]{t1, t2, t3, ..., tn}).

这篇关于什么语法&QUOT;最后的字符串参数... args&QUOT;意思/怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆