A
A
Alexander2014-04-04 10:55:01
Java
Alexander, 2014-04-04 10:55:01

How to add element (field) to ArrayList right after initialization?

How to add an element (field) to an ArrayList as soon as it is initialized?

contactList.put(Name, new ArrayList<AdapterRowItem>());
contactList.get(Name).add(item);

You need to add one element at once (AdapterRowItem) item; not to call get on the HashMap.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Ulyanov, 2014-04-04
@UserAlexandr

new ArrayList(Arrays.asList(item));

A
Artyushov, 2014-04-04
@Artyushov

You can also use new ArrayList<>() {{add(item);}}

V
vans239, 2014-04-04
@vans239

Are you sure that in this case you need to put the question like this?
Judging by the use-case, you write a bike: Multimap.
docs.guava-libraries.googlecode.com/git/javadoc/co...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question