We can use the addAll() method of List to add two lists.
boolean addAll(Collection<? extends E> c)
Appends all elements in the specified collection to the end of this list in the order returned by the iterator of the specified collection (optional operate). If the specified collection is modified while the operation is in progress, the behavior of the operation is undefined. (Note that this will happen if the specified collection is this list and it is non-empty.).
c - The collection containing the elements to be added to this list.
Returns True if this list changed as a result of the call.
UnsupportedOperationException - if this list does not support the addAll operation.
ClassCastException - If the class of an element of the specified collection prevents it from being added to this list.
boolean addAll(int index, Collection<? extends E> c)
Insert all elements in the specified collection into the specified position of this list (optional operation). Moves the element currently at that position (if any) and any subsequent elements to the right (increases their index). New elements will appear in this list in the order returned by the iterator of the specified collection. If the specified collection is modified while the operation is in progress, the behavior of the operation is undefined. (Note that this will occur if the specified collection is this list and it is non-empty.)
index< /strong> - Index of inserting the first element from the specified collection.
#c - The collection containing the elements to be added to this list.
Returns True if this list changed as a result of the call.
UnsupportedOperationException - if this list does not support the addAll operation.
ClassCastException< /strong> - if the class of an element of the specified collection prevents it from being added to this list.
NullPointerException - If the specified collection contains one or more null elements and this list does not allow null elements, or the specified collection is null.
IllegalArgumentException - If an element of the collection specified by a property prevents it from being added to this list.
IndexOutOfBoundsException - if the index is out of bounds (index < 0 | | index > size()).
The following example shows how to add two lists using the addAll() method -
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); System.out.println("List: " + list); List<String> list1 = new ArrayList<>(); list1.add("D"); list1.add("E"); list1.add("F"); System.out.println("List1: " + list1); list.addAll(list1); System.out.println("Updated List: " + list); List<String> list2 = new ArrayList<>(); list2.add("G"); list2.add("H"); list2.add("I"); list2.addAll(0, list); System.out.println("List2: " + list2); } }
This will produce The following results-
List: [A, B, C] List1: [D, E, F] Updated List: [A, B, C, D, E, F] List2: [A, B, C, D, E, F, G, H, I]
The above is the detailed content of In Java, how to add two lists?. For more information, please follow other related articles on the PHP Chinese website!