Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Removing Element from the Specified Index in Java ArrayList
Next article icon

How to Insert all the Collection Elements to the Specified Position in Java ArrayList?

Last Updated : 22 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The element can be inserted at the collection elements to the specified position in ArrayList using Collection.addAll() method which is present in java.util.ArrayList class. If any element present at the index then that element and all its right side elements are shifted to the right side. this method accepts two arguments the first argument is the index where we want to insert the elements and the second argument is the object of another collection. This method returns a boolean value true if elements are successfully inserted else returns false.

addAll(Collection c) : This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress(implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty).

Syntax:

public boolean addAll(int i, Collection c);

Exception: This method throws NullPointerException if elements contain one or more null values and c does not permit null elements, or if c or elements are null

Exception: Here two types of exceptions are possible.

  1. IndexOutOfBoundsException − If the index is out of range then it will show Index Out of Bounds Exception.
  2. NullPointerException − If the specified collection is null then it will show Null Pointer Exception.

CODE:

Java
// Java Program to insert Collection element at // specified index  // Importing ArrayList class and Vector class  // of java.util package import java.util.ArrayList; import java.util.Vector;  public class GFG {      // Main driver method     public static void main(String[] args)     {          // Create an ArrayList         ArrayList<String> ArrList = new ArrayList<String>();          // Adding elements in above ArrayList created       // Custom inputs         ArrList.add("Computer");         ArrList.add("Science");         ArrList.add("Portal");         ArrList.add("GeeksforGeeks");          // Display message         System.out.print("The ArrayList is : ");          // Display original ArrayList         System.out.println(ArrList);          // Creating a vector for elements         Vector<String> vector = new Vector<String>();          // Insert elements in vector       // Custom inputs         vector.add("x");         vector.add("y");         vector.add("z");          // Note: Chosen index where added may vary as per         // req Add vector element in arrayList at 1 index         ArrList.addAll(1, vector);          // Display arrayList after insert elements         // at specific indexchosen above         System.out.println(             "List after addition of element at specific index : ");          // Display List after adding element at specific         // index         System.out.println(ArrList);     } } 

Output
The ArrayList is : [Computer, Science, Portal, GeeksforGeeks] List after addition of element at specific index :  [Computer, x, y, z, Science, Portal, GeeksforGeeks]


 


Next Article
Removing Element from the Specified Index in Java ArrayList

R

rastogik346
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Java Programs
  • Technical Scripter 2020
  • Java-ArrayList
Practice Tags :
  • Java

Similar Reads

  • Insert all Elements of Other Collection to Specified Index of Java ArrayList
    ArrayList is part of the collection framework. It is a List and implements the java.util.list interface. ArrayList is a better alternative to Arrays, especially if you are not sure about the array size. Unlike array which has a fixed size, ArrayList can grow in size when needed. Internally ArrayList
    2 min read
  • How to Insert an element at a specific position in an Array in Java
    An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in Java.Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos. Approach 1: Here's how to do it
    4 min read
  • How to Prevent the Addition of Duplicate Elements to the Java ArrayList?
    Ever wondered how you can make an ArrayList unique? Well, in this article we'll be seeing how to prevent the addition of duplicates into our ArrayList. If an ArrayList have three duplicate elements, but at the end, only the ones which are unique are taken into the ArrayList and the repetitions are n
    3 min read
  • Removing Element from the Specified Index in Java ArrayList
    The remove(int index) method present in java.util.ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices). Syntax : public removed_element remove(int index) Parameters: The index of the element
    3 min read
  • How to Swap Two Elements in an ArrayList in Java?
    We can swap two elements of Array List using Collections.swap() method. This method accepts three arguments. The first argument is the ArrayList and the other two arguments are the indices of the elements. This method returns nothing.  Syntax: public static void swap(List list, int a, int b); Parame
    2 min read
  • Java Program to Add the Data from the Specified Collection in the Current Collection
    The grouping of objects in a single unit is called a collection. For example Array, Lists, Hashsets, etc. Data can be added from a specified collection in the current collection by using the addAll() method in Java. This method falls under the header file, java.util.*. The addAll() method returns a
    3 min read
  • How to Replace an Element at a Specific Index of the Vector in Java?
    The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. Examples Input : Vector= ["
    2 min read
  • How to Add an Element at Particular Index in Java ArrayList?
    ArrayList.add() method is used to add an element at particular index in Java ArrayList. Syntax: public void add(int index, Object element) ; Parameters: index -position at which the element has to be inserted. The index is zero-based.element - the element to be inserted at the specified position. Ex
    2 min read
  • How to Copy and Add all List Elements to an Empty ArrayList in Java?
    We can copy and add List items in Array List using addAll() method. This method accepts a Collection (Ex. List) as an argument and adds the collection items at the end of its calling Collection (Ex. ArrayList). This method returns a boolean value. addAll() return true if the collection successfully
    2 min read
  • How to Convert ArrayList to LinkedHashSet in Java?
    ArrayList is a data structure that overcomes the shortcomings of the common array in Java wherein the size has to be explicitly specified beforehand. The length of the array data structure cannot be modified which is taken care of the ArrayList data structure. This data structure is also known as th
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences