转换'的ArrayList<串GT;到'字符串[]'在Java中 [英] Converting 'ArrayList<String> to 'String[]' in Java

查看:128
本文介绍了转换'的ArrayList<串GT;到'字符串[]'在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会如何转换的的ArrayList<弦乐> 对象Java中的的String [] 阵列


解决方案

 列表<串GT;名单= ..;
String []数组= list.toArray(新的String [0]);

例如:

 列表<串GT;名单=新的ArrayList<串GT;();
//添加一些东西
list.add(机器人);
list.add(苹果);
的String []字符串数组= list.toArray(新的String [0]);

不传递任何参数返回的toArray()方法对象[] 。所以,你必须传递一个数组作为参数,它将从列表中填充数据,并返回。你可以传递一个空数组为好,但你也可以通过具有所需大小的数组。

重要更新:本来code以上使用新的String [则为list.size()] 。但是,此博文显示,由于JVM的优化,使用新的String [ 0] 不如现在。

How might I convert an ArrayList<String> object to a String[] array in Java?

解决方案

List<String> list = ..;
String[] array = list.toArray(new String[0]);

For example:

List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);

The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.

Important update: Originally the code above used new String[list.size()]. However, this blogpost reveals that due to JVM optimizations, using new String[0] is better now.

这篇关于转换'的ArrayList&LT;串GT;到'字符串[]'在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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