addAll(int, Collection): This method is available in java.util.Vector class of Java.
Syntax:
boolean java.util.Vector.addAll(int index, Collection<? extends K> c)
This method takes one argument. This method inserts all of the elements in the specified Collection into this Vector at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right(increases their indices).
Parameters: One parameter is required for this method.
index: index at which to insert the first element from the specified collection.
c: elements to be inserted into this Vector.
Returns: true if this Vector changed as a result of the call.
Throws:
1. ArrayIndexOutOfBoundsException - if the index is out of range(index < 0 || index > size()).
2. NullPointerException - if the specified collection is null.
Approach 1: When no exception
Java
import java.util.Collection;import java.util.Vector;public class VectoraddAll2 {public static void main(String[] args) {Vector<String> vector = new Vector<>();vector.add("Hello");vector.add("Java");Collection<String> c = new Vector<>();c.add("C++");c.add("Program");int index = 1;System.out.println(vector.addAll(index, c));}}
Output:
true
Approach 2: ArrayIndexOutOfBoundsException
Java
import java.util.Collection;import java.util.Vector;public class VectoraddAll2 {public static void main(String[] args) {Vector<String> vector = new Vector<>();vector.add("Hello");vector.add("Java");Collection<String> c = new Vector<>();c.add("C++");c.add("Program");int index = 5;System.out.println(vector.addAll(index, c));}}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 5 at java.base/java.util.Vector.addAll(Vector.java:1033)
Approach 3: NullPointerException
Java
import java.util.Collection;import java.util.Vector;public class VectoraddAll2 {public static void main(String[] args) {Vector<String> vector = new Vector<>();vector.add("Hello");vector.add("Java");Collection<String> c = null;int index = 1;System.out.println(vector.addAll(index, c));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Collection.toArray()" because "c" is null at java.base/java.util.Vector.addAll(Vector.java:1035)
No comments:
Post a Comment