How addAll(int index, Collection c) Works in a Java ArrayList
With the overloaded addAll(int index, Collection c) method, you can place all elements of a supplied collection into an ArrayList at a position you choose. The call returns a boolean, and any elements already at or after that index are shifted forward.
Signature of the Method
boolean addAll(int index, Collection c);
- index — the spot at which the new elements are placed.
- c — the collection providing the elements to be added.
- Return type — a boolean that reports the result.
- Exception — a NullPointerException occurs if the collection passed in is null.
A Practical Example
In the example below, a list of names is created, a second list is built, and that second list is inserted into the first at index 1. The elements that follow move along to free up space.
import java.util.ArrayList;
public class ArraylistExample {
public static void main(String[] args) {
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add("JAVA");
listOfNames.add("GOAL");
listOfNames.add("RAVI");
ArrayList<String> listOfNames2 = new ArrayList<String>();
listOfNames2.add("NEW");
listOfNames2.add("LIST");
listOfNames.addAll(1, listOfNames2);
for (String name : listOfNames) {
System.out.println("Names from list = " + name);
}
}
}Result
Names from list = JAVA
Names from list = NEW
Names from list = LIST
Names from list = GOAL
Names from list = RAVI
See more options on the Java ArrayList methods page, or jump back to the article discussion.
Explore Java Topics
Visit the JavaGoal category for a guided learning path that takes you through Java from the fundamentals up to advanced features.
- Data Types in Java
- Java Keywords
- Operators in Java
- What is a Method in Java
- Control Structures in Java
- Java Control Statements
- if Statement in Java
- Switch Statement in Java
- Java Loops
- break Statement in Java
- continue Statement in Java
- Array in Java
- OOPs Concepts in Java
- Class and Object in Java
- Constructor in Java
- Constructor Overloading in Java
- Constructor Chaining in Java
- Inheritance in Java
- Multiple Inheritance in Java
- Polymorphism in Java
- Method Overloading in Java
- Method Overriding in Java
- Abstraction in Java
- Abstract Class in Java
- Interface in Java
- Multiple Inheritance Using Interface
- Encapsulation in Java
- Association in Java
- Access Modifiers in Java
- Packages in Java
- Java import Package
- enum in Java
- Wrapper Class in Java
- Immutable Class in Java
- final Keyword in Java
- final Variable in Java
- final Method in Java
- final Class in Java
- final, finally and finalize
- static Keyword in Java
- Static Variable in Java
- Static Method in Java
- Static Block in Java
- Static Class in Java
- this Keyword in Java
- Super Keyword in Java
- import in Java
- Nested Classes in Java
- Multithreading in Java
- Thread Life Cycle in Java
- Thread Scheduler in Java
- Java Thread Sleep
- join() Method in Java
- yield() Method in Java
- wait() Method in Java
Keep in Touch
Talk to us in chat, follow our Facebook page, or subscribe to our social channels to get new Java tutorials and videos.
Chat Now FacebookWhen you insert at an index, the ArrayList keeps the order of the incoming collection and moves the existing elements to the right. If the index is out of range, the call fails with an IndexOutOfBoundsException, so it is worth checking the position before you add.
