- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert all elements of other Collection to Specified Index of Java ArrayList
The elements of a Collection can be inserted at the specified index of the ArrayList using the method java.util.ArrayList.addAll(). This method has two parameters i.e. the specific index at which to start inserting the Collection elements in the ArrayList and the Collection itself.
If there is an element already present at the index specified by ArrayList.addAll() then that element and all subsequent elements are shifted to the right to make the space for the Collection elements in the ArrayList.
A program that demonstrates this is given as follow.
Example
import java.util.ArrayList; import java.util.Vector; public class Demo { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); aList.add("John"); aList.add("Sally"); aList.add("Harry"); aList.add("Martha"); aList.add("Susan"); Vector<String> vec = new Vector<String>(); vec.add("Peter"); vec.add("Michael"); vec.add("Jenna"); aList.addAll(2, vec); System.out.println("The ArrayList elements are: " + aList); } }
Output
The output of the above program is as follows
The ArrayList elements are: [John, Sally, Peter, Michael, Jenna, Harry, Martha, Susan]
Now let us understand the above program.
First, the ArrayList is defined and elements are added using the ArrayList.add() method. A code snippet which demonstrates this is as follows
ArrayList<String> aList = new ArrayList<String>(); aList.add("John"); aList.add("Sally"); aList.add("Harry"); aList.add("Martha"); aList.add("Susan");
Then the vector is defined and elements are added. After that the ArrayList.addAll() method is used to insert all the vector elements in the ArrayList starting at index 2. Then the ArrayList elements are displayed. A code snippet which demonstrates this is as follows
Vector<String> vec = new Vector<String>(); vec.add("Peter"); vec.add("Michael"); vec.add("Jenna"); aList.addAll(2, vec); System.out.println("The ArrayList elements are: " + aList);
- Related Articles
- Java Program to insert all elements of other Collection to specified Index of ArrayList
- Append all elements of other Collection to ArrayList in Java
- Java Program to append all elements of other Collection to ArrayList
- How to insert the elements of a collection into the List at the specified index in C#?
- Add an element to specified index of ArrayList in Java
- Insert an element into Collection at specified index in C#
- Insert an element into the ArrayList at the specified index in C#
- Replace all occurrences of specified element of ArrayList with Java Collections
- How to remove all elements of ArrayList in Java?
- Reverse order of all elements of ArrayList with Java Collections
- Replace All Elements Of ArrayList with with Java Collections
- Remove all the elements from an ArrayList that are in another Collection in Java
- Remove element at specified index of Collection in C#
- Swift Program to insert multiple elements into the array from the specified index
- Golang Program to insert multiple elements into the array from the specified index
