3:40 am - Friday May 26, 2017

Inserts all of the elements in the specified collection into this list

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This program is used to insert all the elements in the specified list.

Example Program


import java.util.ArrayList;

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

// create an empty array list1 with an initial capacity
ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

// use add() method to add elements in the list
arrlist.add(12);
arrlist.add(20);
arrlist.add(45);

// let us print all the elements available in list1
System.out.println("Printing list1:");
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}

// create an empty array list2 with an initial capacity
ArrayList<Integer> arrlist2 = new ArrayList<Integer>(5);

// use add() method to add elements in list2
arrlist2.add(25);
arrlist2.add(30);
arrlist2.add(31);
arrlist2.add(35);

// let us print all the elements available in list2
System.out.println("Printing list2:");
for (Integer number : arrlist2) {
System.out.println("Number = " + number);
}

// inserting all elements, list2 will get printed after list1
arrlist.addAll(2, arrlist2);

System.out.println("Printing all the elements");
// let us print all the elements available in list1
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

 

Explanation:

public boolean addAll(int index, Collection<? extends E> c)

Inserts all of the elements in the specified collection into this list, starting at the specified position. 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.

Specified by:

addAll in interface List<E>

Overrides:

addAll in class AbstractList<E>

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:

IndexOutOfBoundsException – if the index is out of range (index < 0 || index > size())

NullPointerException – if the specified collection is null

BACK

 

 

Filed in: Z-A All Java Codes

Comments are closed.