Site icon

Add/ insert elements/String objects to arraylist collection (java/ example)

  1. Given an arraylist collection, add or insert elements/Objects to arraylist collection.
  2. ArrayList class is resizable array implementation of the List interface.
  3. ArrayList maintains the insertion order of element or string objects.
  4. ArrayList allows the duplicate elements & we can randomly access elements by index.
  5. ArrayList is not thread safe.
    • If multiple threads access an ArrayList instance concurrently, then arraylist must be synchronized externally.

ArrayList collection has following methods to add or insert elements/objects.

No.Method Name Description
1boolean add(E e) Appends the specified element to the end of this list.
2void add(int index, E element) Inserts the specified element at the specified position in this list.
3boolean addAll(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
4boolean addAll(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list, starting at the specified position.

1. Class hierarchy of ArrayList collection in java:

Fig 1: Class hierarchy of ArrayList collection

2. Program – Add/insert elements/String objects to arraylist (java/ example)

package org.learn.collection.list.arrayList;

import java.util.ArrayList;

public class DemoAddToArrayList {

	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<>();
		arrayList.add("badminton");
		arrayList.add("boxing");
		arrayList.add("diving");

		System.out.println("Demo of add methods: ");
		demoAddMethod(arrayList);
	}

	private static void demoAddMethod(ArrayList<String> arrayList) {
		System.out.println("Orignal ArrayList:" + arrayList);
		arrayList.add(0, "archery");
		arrayList.add(2, "canoe");
		// [archery, badminton, canoe, boxing, diving]
		System.out.println("1. Added element at 0,2 index: " + arrayList);

		arrayList.add("diving");
		// [archery, badminton, canoe, boxing, diving, diving]
		System.out.println("2. Added diving to list: " + arrayList);

		ArrayList<String> addElementsList = new ArrayList<>();
		addElementsList.add("squash");
		addElementsList.add("bowling");

		// It will add elements to last of list
		arrayList.addAll(addElementsList);
		//[archery, badminton, canoe, boxing, diving, diving, squash, bowling]
		System.out.println("3. Added another list : " + arrayList);

		ArrayList<String> anotherList = new ArrayList<>();
		anotherList.add("golf");
		anotherList.add("judo");

		// It will add elements to last of list
		arrayList.addAll(1, anotherList);
		// [archery, golf, judo, badminton, canoe, boxing, diving, diving, squash, bowling]
		System.out.println("4. Added another list at index 1 : " + arrayList);
	}
}

3. Output – Add/insert elements/String objects to arraylist (java/ example)

Demo of add methods: 
Orignal ArrayList:[badminton, boxing, diving]
1. Added element at 0,2 index: [archery, badminton, canoe, boxing, diving]
2. Added diving to list: [archery, badminton, canoe, boxing, diving, diving]
3. Added another list : [archery, badminton, canoe, boxing, diving, diving, squash, bowling]
4. Added another list at index 1 : [archery, golf, judo, badminton, canoe, boxing, diving, diving, squash, bowling]

Oracle reference

Exit mobile version