Java ArrayList addAll() methodJava ArrayList addAll(Collection c) methodThe addAll (Collection c) method of Java ArrayList classappends all of the elements in the specified collection to the end of this list. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. Syntax:Parameter:"c": is the Collection that contained elements to be added to this list. Return:Return "true": if the collection "c" has been appended successfully to the original list. Exception:java.lang.NullPointerException: If the specified collection "c" is null. Example 1Test it NowOutput: [A, B, C] [Apple, Ball, Cat] true [A, B, C, Apple, Ball, Cat] Example 2Test it NowOutput: [Apple, Banana, Orange] Java ArrayListaddAll(int index, Collection c) methodThe addAll (int index, Collection c) method of Java ArrayList classinserts all of the elements in the specified collectioninto this list starting at the specified index. It shifts the element currently at that position and any subsequent elements to the right. The new elements will appear in the list in the order that they are returned by the specified collection?s iterator. Syntax:Parameter:"index":The position at which to insert the first element of the specified list. "c": is the Collection that contained elements to be added to this list. Return:Return "true": If the collection "c" has been inserted successfully to the original list. Exception:java.lang.NullPointerException: If the specified collection "c" is null. java.lang.IndexOutOfBoundsException: If the index is out of range. Example 3Test it NowOutput: [A, B, C] [Adel, Ahmed, Ali] true [A, Adel, Ahmed, Ali, B, C] Example 4Test it NowOutput: [1, 2, 3] 3 java.lang.IndexOutOfBoundsException: Index: 4, Size: 3 Example 5Test it NowOutput: [1, 2, 3] 3 java.lang.NullPointerException Next TopicJava ArrayList |
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