Java LinkedList addAll()方法
java.util.LinkedList.addAll(Collection<? extends E> c) 方法用于将指定集合中的所有元素附加到此LinkedList的末尾,以指定集合的迭代器返回它们的顺序。
1 语法
public boolean addAll(Collection<? extends E> c)
2 参数
c:包含要添加到此列表的元素的集合。
3 返回值
如果此列表由于调用而更改,则此方法返回true。
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.LinkedList.addAll(Collection<? extends E> c)方法的例子
*/
import java.util.*;
public class Demo {
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]
热门文章
优秀文章