addAll ArrayList Java
Ngày đăng:
07/01/2022
Trả lời:
0
Lượt xem:
148
Java ArrayList addAll(int index, Collection c) Method exampleBy Chaitanya Singh | Filed Under: Java Collections In the last tutorial we have shared the example ofaddAll(Collection c) method which is used for adding all the elements of Collection c at the end of list. Here we will see another variant add(int index, Collection c)which adds all the elements of c at the specified index of a list. public boolean addAll(int index, Collection c) ExampleIn this example we have two ArrayList of String type and we are adding the element of second arraylist at the 3rd position(index =2) of first arraylist. package beginnersbook.com; import java.util.ArrayList; public class ExampleOfaddAllMethod { public static void main(String[] args) { // ArrayList1 ArrayListOutput: ArrayList1 before addAll:[Apple, Orange, Grapes, Mango] ArrayList2 content:[Fig, Pear, Banana, Guava] ArrayList1 after adding ArrayList2 at 3rd Pos: [Apple, Orange, Fig, Pear, Banana, Guava, Grapes, Mango]Reference:http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#addAll(int, java.util.Collection) |