- 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
What does the method addAll(int, Coll C) do in java?
The addAll(int index, Collection<? extends E> c) method of the class java.util.ArrayList inserts all of the elements in the specified collection into this list, starting at the specified position. It 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.
Example
import java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { ArrayList<Integer> arrlist = new ArrayList<Integer>(5); arrlist.add(12); arrlist.add(20); arrlist.add(45); System.out.println("Printing list1:"); for (Integer number : arrlist) { System.out.println("Number = " + number); } ArrayList<Integer> arrlist2 = new ArrayList<Integer>(5); arrlist2.add(25); arrlist2.add(30); arrlist2.add(31); arrlist2.add(35); System.out.println("Printing list2:"); for (Integer number : arrlist2) { System.out.println("Number = " + number); } arrlist.addAll(2, arrlist2); System.out.println("Printing all the elements"); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }
Output
Printing list1: Number = 12 Number = 20 Number = 45 Printing list2: Number = 25 Number = 30 Number = 31 Number = 35 Printing all the elements Number = 12 Number = 20 Number = 25 Number = 30 Number = 31 Number = 35 Number = 45
Advertisements