用 List 数据填充数组 [英] Fill a array with List data

查看:33
本文介绍了用 List 数据填充数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用一个 List 提供的数据填充数组?

How can I fill a array with the data provided by one List?

例如,我有一个带字符串的列表:

For example, I have a List with Strings:

List l = new ArrayList<String>();
l.add("a");
l.add("b");
l.add("c");

然后我想将此数据复制到一个字符串数组中:

then I want to copy this data into a String array:

String[] array = ?

推荐答案

有一个 toArray() 方法在列表中...
您必须首先分配一个适当大小的数组(通常是 list.size()),然后将其作为参数传递给 toArray 方法.数组将使用列表内容进行初始化.

There's a toArray() method in list...
You have to first allocate an array of an appropriate size (typically list.size()), and then pass it to the toArray method as a parameter. The array will be initialized with the list content.

例如:

String[] myArray = new String[myList.size]();
myList.toArray(myArray);

你也可以在 toArray 的括号内进行 new 调用

You can also do the new call inside the parentheses of toArray

这篇关于用 List 数据填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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