Java Vector addAll() MethodThe addAll() Java Vector class method inserts all of the elements in the specified collection to the end of the vector which is in use. The order of the elements will be the same as they are returned by the specified collection's iterator. There is two different types of Java addAll() method which can be differentiated depending on its parameter. These are:
addAll(Collection<? extends E> c) Method:This method is used to append all of the elements in the specified collection to the end of this Vector. addAll(int index, Collection<? extends E> c) Method:This method is used to insert all of the elements in the specified collection into the vector which is in use at the specified position. Syntax:Following is the declaration of addAll() method: Parameter:
Returns:The addAll() method returns true if this Vector changed as a result of the call. Exceptions:NullPointerException- This method has thrown an exception if the specified collection is null. IndexOutOfBoundsException- This method has thrown an exception if the index of an array is out of range i.e. (index < 0 || index > size()). Compatibility Version:Java 1.2 and above Example 1:Test it NowOutput: List of first arrayList: [1, 2, 3] List of second arrayList: [4, 5, 6, 7] Final arrayList: [1, 2, 3, 4, 5, 6, 7] Example 2:Test it NowOutput: Final vector list: [A, B, C, D, E, F, G, H] Example 3:Test it NowOutput: Final vector list: [Java, Android, Python, PHP] Example 4:Test it NowOutput: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3 at java.base/java.util.Vector.addAll(Vector.java:1064) at myPackage.VectorAddAllExample4.main(VectorAddAllExample4.java:16) Next TopicJava Vector |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India