加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 语法“final String … args”是什么意思/做什么?

发布时间:2020-12-14 23:47:23 所属栏目:Java 来源:网络整理
导读:这应该是一个相当简单的问题.我环顾四周,找不到有关此语法的任何主题,而“…”使得在Google上搜索变得困难.我正在开发一个简单的测试应用程序,用于将数据库文件从非root用户手机上的受保护位置复制到SD卡上我可以访问以查看 sqlite3 database viewing tool的
这应该是一个相当简单的问题.我环顾四周,找不到有关此语法的任何主题,而“…”使得在Google上搜索变得困难.我正在开发一个简单的测试应用程序,用于将数据库文件从非root用户手机上的受保护位置复制到SD卡上我可以访问以查看 sqlite3 database viewing tool的位置.我知道这似乎是一种迂回的方式做事,但模拟器拒绝在我的上网本上打开,所以我现在用我的手机测试开发.

代码已经写好了,所以我是borrowing it from here并将其调整到我的代码中.我遇到过这一小段代码:

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

我以前从未见过参数String … args.这是什么意思/做什么?

谢谢!
Moscro

解决方法

Java中参数的省略号表示类型的vararg.因此,在您的情况下,…意味着您可以将任意数量的String参数传递给doInBackground方法.

因此,您可以调用此方法doInBackground(“String1”,“String2”,“String3”)或doInBackground(“String1”)或doInBackground(“String1”,“String3”,“String4”)或任何其他形式.

见这里http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

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或更新版本中可用.

参见:http://www.developer.com/java/other/article.php/3323661

正如以上两个链接所提到的,它只不过是含糖的数组.基本上,当你定义myMethod(T … vararg)时,Java会将其视为myMethod(T [] vararg).当你调用myObj.myMethod(t1,t2,t3,…,tn)时,Java将它传递给myObj.myMethod(new T [] {t1,tn}).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读