How to add elements in ArrayList

In this post we will see various ways to add elements in ArrayList in java.

ArrayList provides following methods to add elements.

1. boolean add(E e)

Adds an element at the end of the arrayList.

Example

Output

2. void add(int index, E element)

Adds an element at a specified position in the arrayList

Example

Output

3. boolean addAll(Collection<? extends E> c)

Adds all the elements that are specified in the collection at the end of the ArrayList.

Example

Output

4. boolean addAll(int index, Collection<? extends E> c)

Adds all the elements that are specified in the collection at a specified position in the arrayList.

Example

Output

Reference

1. ArrayList

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn