java 如何将 File [] Array 内容添加到 ArrayList 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16437846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add File [] Array content into ArrayList?
提问by vijayk
I have an array:
我有一个数组:
File [] temp=null;
And I have an arrayList
of File
type:
我有一个arrayList
的File
类型:
List <File> tempList = new ArrayList <File>();
Now I want to add the content from temp
to tempList
. So anyone can please tell me How do I this?
现在我想从temp
to添加内容tempList
。所以任何人都可以告诉我我该怎么做?
采纳答案by Sanjaya Liyanage
Try this
试试这个
tempList.addAll(Arrays.asList(temp));
回答by Adrian Shum
If you are not going to update the content of the array (add/removing element), it can be as simple as
如果你不打算更新数组的内容(添加/删除元素),它可以很简单
List<File> tempList = Arrays.asList(temp);
Of course, if you want a list that you can further manipulate, you can still do something like
当然,如果你想要一个可以进一步操作的列表,你仍然可以做类似的事情
List<File> tempList = new ArrayList<File>(Arrays.asList(temp));
回答by ajduke
use following
使用以下
List<File>tempList = Arrays.asList(temp);
回答by The Cat
You can iterate through the array and add each element to the list.
您可以遍历数组并将每个元素添加到列表中。
for (File each : temp)
tempList.add(each);
回答by eldris
You can use a collections library call for this:
您可以为此使用集合库调用:
Arrays.asList(temp);