Home » Java programming language

Java LinkedList boolean addAll(int index, Collection c) method with Example

Java LinkedList boolean addAll(int index, Collection c) method: Here, we are going to learn about the boolean addAll(int index, Collection c) method of LinkedList class with its syntax and example.
Submitted by Preeti Jain, on June 16, 2019

LinkedList boolean addAll(int index, Collection c) method

  • This method is available in package java.util.Collection and here, Collection is an interface.
  • In this method, the index is the position where we have to start inserting the Collection elements and Collection is the list of elements whether Collection is of ArrayList, LinkedList type, etc.
  • This method is used to insert the collection of objects at the specified position in the linked list.
  • Index position starts from 0.

Syntax:

    boolean addAll(int index , Collection c){
    }

Parameter(s):

We can pass two arguments as a parameter in the method and the Collection parameter is the collection of elements will add from the starting position of the specified index in the linked list.

Return value:

The return type of this method is boolean that means this method returns true during execution of at least inserting of one element.

Java program to demonstrate example of LinkedList addAll(int index, Collection c) method

import java.util.LinkedList;
import java.util.ArrayList;

public class LinkList {
 public static void main(String args[]) {

  // Creating a LinkedList 
  LinkedList list = new LinkedList();

  // Use add() method to add objects 
  // in the LinkedList
  list.add("J");
  list.add("A");
  list.add("V");
  list.add("A");


  // Creating a Collection ArrayList object 
  // of ArrayList Type
  ArrayList al = new ArrayList();
  al.add("P");
  al.add("R");
  al.add("O");
  al.add("G");

  al.add("R");
  al.add("A");
  al.add("M");
  al.add("M");
  al.add("I");
  al.add("N");
  al.add("G");
  // Displaying the Current LinkedList 
  System.out.println("The Current LinkedList is: " + list);

  // Appending the collection ArrayList to the 
  // LinkedList and ArrayList will start inserting 
  // from index 4(i.e. From Fifth Position )
  list.addAll(4, al);

  //  Displaying the new LinkedList
  System.out.println("The new linked list is: " + list);
 }
}

Output

D:\Programs>javac LinkList.java

D:\Programs>java LinkList
The Current LinkedList is: [J, A, V, A]
The new linked list is: [J, A, V, A, P, R, O, G, R, A, M, M, I, N, G]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.