Java ArrayList addAll(int index, Collection c) method with example

Java ArrayList addAll(int index, Collection<? extends E> c) method Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection’s iterator.

Method Signature

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

Specified by:

addAll method in interface List

Overrides

Overrides: addAll method in class AbstractList

Parameters:

index – index at which to insert the first element from the specified collectionc – collection containing elements to be added to this list

Returns:

true if this list changed as a result of the call

Throws Exception:

IndexOutOfBoundsException – if the index is out of range (index size())

NullPointerException – if the specified collection is null

ArrayList addAll(int index, Collection<? extends E> c ) method Implementation

 public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount

        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);

        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

private method rangeCheckForAdd() called to given index not exceeds the existing list size. If index given is greater than list size IndexOutOfBoundsException will be thrown

private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

private method ensureCapacityInternal() called to check the list reached its initial size and increase the size if need, before Java 8 List will be initialized with 10 empty elements and keep on resize the capacity when new element add. Its occupy some unnecessary memory. In Java 8 this default initial size changed to zero, check more about this in Java 8 tutorials.

 private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }

Java ArrayList addAll(int index, Collection<? extends E> c ) method example

package com.ehowtonow.java.util.arraylist;

import java.util.ArrayList;

public class AddAllExample1 {

	public static void main(String[] args) {

		// create empty array list with Integer type
		ArrayList<Integer> arrayList1 = new ArrayList<Integer>();

		// add() method to add Integer element in the list
		arrayList1.add(10);
		arrayList1.add(15);
		arrayList1.add(20);
		arrayList1.add(25);
		arrayList1.add(30);

		// print the values of arrayList1
		for (Integer numbers : arrayList1) {
			System.out.println(" arrayList1 : " + numbers);
		}

		// create empty array list with Integer type
		ArrayList<Integer> arrayList2 = new ArrayList<Integer>();

		// add() method to add Integer element in the list
		arrayList2.add(40);
		arrayList2.add(45);
		arrayList2.add(50);
		arrayList2.add(55);
		arrayList2.add(60);

		// print the values of arrayList2
		for (Integer numbers : arrayList2) {
			System.out.println(" arrayList2 : " + numbers);
		}

		arrayList2.addAll(5, arrayList1);

		/*
		 * print the values of arrayList2 after all the element from arrayList1 added at
		 * index 5 (end of arraylist 2)
		 */
		for (Integer numbers : arrayList2) {
			System.out.println(" arrayList2 after all All: " + numbers);
		}

		// create empty array list with initial capacity
		ArrayList<Integer> arrayList3 = new ArrayList<Integer>(3);
		arrayList3.add(65);
		arrayList3.add(70);

		// print the values of arrayList3
		for (Integer numbers : arrayList3) {
			System.out.println(" arrayList3 : " + numbers);
		}

		arrayList2.addAll(5, arrayList3);

		/*
		 * print the values of arrayList3 after all the element from arrayList3 added at
		 * index 5 (now index is not at end)
		 */
		for (Integer numbers : arrayList2) {
			System.out.println(" arrayList2 after all All: " + numbers);
		}

		ArrayList<Integer> arrayList4 = new ArrayList<Integer>(3);
		arrayList4.add(75);

		// This will throw IndexOutOfBoundsException
		arrayList2.addAll(13, arrayList3);

		/*
		 * This will throw IndexOutOfBoundsException 
		 * ArrayList<Integer> arrayList5 = null;
		 *  arrayList3.addAll(arrayList5);
		 */
	}

}

Output

arrayList1 : 10
Exception in thread “main” arrayList1 : 15
arrayList1 : 20
arrayList1 : 25
arrayList1 : 30
arrayList2 : 40
arrayList2 : 45
arrayList2 : 50
arrayList2 : 55
arrayList2 : 60
arrayList2 after all All: 40
arrayList2 after all All: 45
arrayList2 after all All: 50
arrayList2 after all All: 55
arrayList2 after all All: 60
arrayList2 after all All: 10
arrayList2 after all All: 15
arrayList2 after all All: 20
arrayList2 after all All: 25
arrayList2 after all All: 30
arrayList3 : 65
arrayList3 : 70
arrayList2 after all All: 40
arrayList2 after all All: 45
arrayList2 after all All: 50
arrayList2 after all All: 55
arrayList2 after all All: 60
arrayList2 after all All: 65
arrayList2 after all All: 70
arrayList2 after all All: 10
arrayList2 after all All: 15
arrayList2 after all All: 20
arrayList2 after all All: 25
arrayList2 after all All: 30
java.lang.IndexOutOfBoundsException: Index: 13, Size: 12
at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:665)
at java.util.ArrayList.addAll(ArrayList.java:605)
at com.ehowtonow.java.util.arraylist.AddAllExample1.main(AddAllExample1.java:73)


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java ArrayList clear() method with example
  2. Java ArrayList removeAll() method with example
  3. Java ArrayList forEach() method with example
  4. Java ArrayList remove(Object o) method with example
  5. Java ArrayList add(int index, E element) method with example
  6. Java ArrayList ensureCapacity() method with example
  7. Java ArrayList addAll(int index, Collection c) method with example
  8. Java ArrayList add(E e) method with example
  9. Java ArrayList get() method with example
  10. Java ArrayList contains() method with example
  11. Java ArrayList addAll(Collection< ? extends E> c) method with example
  12. Java ArrayList indexOf() method with example
  13. Java ArrayList isEmpty() method with example
  14. Java ArrayList clone() method with example
  15. Java ArrayList remove() method with example

Be the first to comment

Leave a Reply