當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.util.LinkedList.addAll()用法及代碼示例


描述

這個java.util.LinkedList.addAll(Collection<? extends E> c)方法按照指定集合的​​迭代器返回的順序將指定集合中的所有元素追加到此列表的末尾。

聲明

以下是聲明java.util.LinkedList.addAll()方法

public boolean addAll(Collection<? extends E> c)

參數

c- 包含要添加到此列表的元素的集合

返回值

如果此列表因調用而更改,則此方法返回 true

異常

NullPointerException- 如果指定的集合為空

示例

下麵的例子展示了 java.util.LinkedList.addAll() 方法的用法。

package com.tutorialspoint;

import java.util.*;

public class LinkedListDemo {
   public static void main(String[] args) {

      // create a LinkedList
      LinkedList list = new LinkedList();

      // add some elements
      list.add("Hello");
      list.add(2);
      list.add("Chocolate");
      list.add("10");

      // print the list
      System.out.println("LinkedList:" + list);


      // create a new collection and add some elements
      Collection collection = new ArrayList();
      collection.add("One");
      collection.add("Two");
      collection.add("Three");

      // append the collection in the LinkedList
      list.addAll(collection);

      // print the new list
      System.out.println("LinkedList:" + list);
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

LinkedList:[Hello, 2, Chocolate, 10]
LinkedList:[Hello, 2, Chocolate, 10, One, Two, Three]

相關用法


注:本文由純淨天空篩選整理自 Java.util.LinkedList.addAll() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。