
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do I insert elements at a specific index in Java list?
Solution
We can add all elements of one list into another list at a specified index easily using its addAll() method.
Syntax
boolean addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list at the specified position (optional operation). 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 this list in the order that they are returned by the specified collection's iterator.
Type Parameter
E - The runtime type of the collection.
Parameters
index − Index at which to insert the first element from the specified collection.
c − Collection containing elements to be added to this list.
Returns
True if this list changed as a result of the call
Throws
UnsupportedOperationException − If the addAll operation is not supported by this list.
ClassCastException − If the class of an element of the specified collection prevents it from being added to this list.
NullPointerException − If the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null.
IllegalArgumentException − If some property of an element of the specified collection prevents it from being added to this list.
IndexOutOfBoundsException − If the index is out of range (index < 0 || index gt; size()).
Example
The following example shows how to add all elements from a list into another list at a particular index using the addAll() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List<Integer> list = new ArrayList<>(); // add elements to the list list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); // print the list System.out.println(list); List<Integer> list1 = new ArrayList<>(); list1.add(0); list1.addAll(0,list); System.out.println(list1); } }
Output
This will produce the following result −
[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 0]
- Related Articles
- How do I insert elements in a Java list?
- How do I insert all elements from one list into another in Java?
- How do I remove multiple elements from a list in Java?
- How to insert the elements of a collection into the List at the specified index in C#?
- How do I insert an item between two items in a list in Java?
- Python Pandas - Insert a new index value at a specific position
- Python – Check if elements in a specific index are equal for list elements
- How do I replace a character at a particular index in JavaScript?
- How do I empty a list in Java?
- How do I search a list in Java?
- How to insert an object in an ArrayList at a specific position in java?
- How can I find elements in a Java List?
- Java Program to Replace a Character at a Specific Index
- Insert to specific index for MongoDB array?
- Golang program to insert a node at the ith index node, when the index is at the mid-index position in the linked list.
