分配数组在Java中的ArrayList [英] Assigning an array to an ArrayList in Java

查看:127
本文介绍了分配数组在Java中的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有可能的数组分配给Java的一个ArrayList

I was wondering if it is possible to assign an array to an ArrayList in Java.

推荐答案

您可以使用<一个href=\"http://download.oracle.com/javase/1.4.2/docs/api/java/util/Arrays.html#asList%28java.lang.Object%5b%5d%29\"><$c$c>Arrays.asList():

Type[] anArray = ...
ArrayList<Type> aList = new ArrayList<Type>(Arrays.asList(anArray));

或可替换地,<一href=\"http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#addAll%28java.util.Collection,%20T...%29\"><$c$c>Collections.addAll():

ArrayList<Type> aList = new ArrayList<Type>();
Collections.addAll(theList, anArray); 

请注意,你是不是技术上分配一个数组列表(当然,你不能做到这一点),但我认为这是导致你正在寻找结束。

Note that you aren't technically assigning an array to a List (well, you can't do that), but I think this is the end result you are looking for.

这篇关于分配数组在Java中的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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