0

i know how to add many things into arraylist at once, like:


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?

asked May 11, 2011 at 21:29
0

2 Answers 2

5

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

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

pickypg
22.4k5 gold badges74 silver badges84 bronze badges
answered May 11, 2011 at 21:30

1 Comment

Similarly, you could use List<String> list1 = Arrays.<String>asList(things); to do it one one step rather than adding.
2

Are you looking for this:

List<String> thingsList = Arrays.asList(things); 
answered May 11, 2011 at 21:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.