collection.toArray(new String[0])中new String[0]的作用

collection.toArray(new String[0])中new String[0]的作用collection.toArray(newString[0])中newString[0]的作用转载2016年02月19日14:35:163372newstring[0]的作用比如:String[]result=set.toArray(newString[0]);Collection的公有方法中,toArray()是比较重要的一个。但是使用无参数的toArr…

大家好,又见面了,我是你们的朋友全栈君。

collection.toArray(new String[0])中new String[0]的作用
转载 2016年02月19日 14:35:16 3372
new string[0]的作用

比如:String[] result = set.toArray(new String[0]);

Collection的公有方法中,toArray()是比较重要的一个。
但是使用无参数的toArray()有一个缺点,就是转换后的数组类型是Object[]。 虽然Object数组也不是不能用,但当你真的想用一个具体类型的数组,比如String[]时,问题就来了。而把Object[]给cast成String[]还是很麻烦的,需要用到这个:

String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);

不管是从哪方面看还是一开始就弄成String[]比较好。

具体怎么办呢?其实用带参数的toArray就好了。官方是这样给出的例子:
String[] a = c.toArray(new String[0]);

如果指定的数组能容纳该 collection,则返回包含此 collection 元素的数组。否则,将根据指定数组的运行时类型和此 collection 的大小分配一个新数组。这里给的参数的数组长度是0,因此就会返回包含此 collection 中所有元素的数组,并且返回数组的类型与指定数组的运行时类型相同。

像 toArray 方法一样,此方法充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。更进一步说,此方法允许在输出数组的运行时类型上进行精确控制,并且在某些情况下,可以用来节省分配开销。

假定 l 是只包含字符串的一个已知 List。以下代码用来将该列表转储到一个新分配的 String 数组:

 String[] x = (String[]) v.toArray(new String[0]);

注意,toArray(new Object[0]) 和 toArray() 在功能上是相同的。

参数:
a – 存储此 collection 元素的数组(如果其足够大);否则,将为此分配一个具有相同运行时类型的新数组。

Collection Interface Array Operations
The toArray methods are provided as a bridge between collections and older APIs that expect arrays on input. The array operations allow the contents of a Collection to be translated into an array. The simple form with no arguments creates a new array of Object. The more complex form allows the caller to provide an array or to choose the runtime type of the output array.

For example, suppose that c is a Collection. The following snippet dumps the contents of c into a newly allocated array of Object whose length is identical to the number of elements in c.

Object[] a = c.toArray();
Suppose that c is known to contain only strings (perhaps because c is of type Collection). The following snippet dumps the contents of c into a newly allocated array of String whose length is identical to the number of elements in c.

String[] a = c.toArray(new String[0]);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146664.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 从零到熟悉,带你掌握Python len() 函数的使用

    从零到熟悉,带你掌握Python len() 函数的使用摘要:本文为你带来如何找到长度内置数据类型的使用len()使用len()与第三方数据类型提供用于支持len()与用户定义的类。本文分享自华为云社区《在Python中使用len()函数》,

    2022年7月5日
    19
  • golang获取UUID[通俗易懂]

    golang获取UUID[通俗易懂]packagemainimport( “fmt” uuid”github.com/satori/go.uuid”)funcmain(){ //创建 u1:=uuid.NewV4() fmt.Printf(“UUIDv4:%s\n”,u1) //解析 u2,err:=uuid.FromString(“f5394eef-e576-4709-9e…

    2022年8月9日
    82
  • 发现制作CSS导航菜单工具CSS Tab Designer

    发现制作CSS导航菜单工具CSS Tab Designer

    2021年7月24日
    53
  • linux的nohup命令

    linux的nohup命令在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用&在程序结尾来让程序自动运行。比如我们要运行mysql在后台:/usr/local/mysql/bin/mysqld_safe–user=mysql&。可是有很多程序并不想mysqld一样,这样我们就需要nohup命令,怎样使用nohup命令呢?这里讲解nohup命令的一些用法。nohup/root/start.sh&在shell中回车后提示:[~]$appendingoutp

    2025年5月24日
    1
  • notepad主题_python主题更改

    notepad主题_python主题更改转载于:点击这里查看原博客更改jupyternotebook的主题颜色(theme)包括pycharmpython/

    2022年8月27日
    6
  • tomcat7官网下载

    tomcat7官网下载1.官网地址:tomcat.apache.org,进入后点击Tomcat72.选择不同的类型,以“64-bitWindowszip”为例3.保存文件,确定4.解压后,进入bin目录,双击startup.bat,出现下图5.打开浏览器,输入127.0.0.1:8080,出现下图,安装成功转载于:https://my.oschina.net/u/4052883/blog/29915522…

    2022年5月19日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号