Home >Java >javaTutorial >In Java, how to add two lists?

In Java, how to add two lists?

WBOY
WBOYforward
2023-09-02 15:05:051202browse

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)

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.

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

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 - 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 - 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 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);
   }
}

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]

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete