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

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

问题描述

如何使用一个列表提供的数据填充数组?

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的括号内执行新调用

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

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

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