Home > Java > javaTutorial > body text

In Java, how to add two lists?

WBOY
Release: 2023-09-02 15:05:05
forward
1167 people have browsed it

In Java, how to add two lists?

We can use the addAll() method of List to add two lists.

Use the addAll() method without an index parameter

boolean addAll(Collection<? extends E> c)
Copy after login

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.).

Parameters

  • c - The collection containing the elements to be added to this list.

Returns

Returns True if this list changed as a result of the call.

Throws

  • 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.

  • < p>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.

  • < p>IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this list.

Use the addAll() method with index parameters

boolean addAll(int index, Collection<? extends E> c)
Copy after login

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.)

Parameters

  • 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

Returns True if this list changed as a result of the call.

Throws

  • 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()).

Example

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);
   }
}
Copy after login

Output

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]
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template