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

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

问题描述

我怎么能填充阵列由一个清单中提供的数据?

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

 列表L =新的ArrayList<串GT;();
l.add(一);
l.add(B);
l.add(C);

那么我想这个数据复制到一个String数组:

  String []数组=?


解决方案

有一个<一个href=\"http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html#toArray%28T%5b%5d%29\">toArray()方法列表...结果
你必须先分配适当大小的阵列(通常则为list.size()),然后将其传递给的toArray方法作为参数。该阵列将与列表内容进行初始化。

例如:

 的String [] myArray的=新的String [myList.size]();
myList.toArray(myarray的);

您也可以做的toArray的括号

里面的新的呼叫

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 = ?

解决方案

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.

For example:

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

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

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

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