简体   繁体   English

添加到阵列列表

[英]Adding to Arraylists

i know how to add many things into arraylist at once, like: 我知道如何一次将许多东西添加到arraylist中,例如:


String [] things = {"eggs ", "pie ", "lasers ", "hat "}; List list1 = new ArrayList();

for (String s: things) list1.add(s);

but is there a way to add things into arraylist iteratively one by one? 但是有一种方法可以迭代地将事物逐个添加到arraylist中吗?

You ARE adding things iteratively, but if what you meant was the exact opposite 您正在迭代添加事物,但是如果您的意思恰恰相反

then if the elements are in any kind of Collection, you can use adAll(Collection c) or if it's an array you can use Arrays.toList(...) to convert the array to a list that you can pass to adAll 那么如果元素在任何种类的Collection中,则可以使用adAll(Collection c);或者如果它是一个数组,则可以使用Arrays.toList(...)将数组转换为可以传递给adAll的列表

http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html

您在寻找这个吗?

List<String> thingsList = Arrays.asList(things); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM