是否可以连接两个或更多的arraylist?我有以下案例:
private Arraylist<Double> getContextVectorsize(int sizeContext){
ArrayList<Double> contextList = new ArrayList<Double>();
for(int i =0 ; i< sizeContext; i++){
contextList = //TODO
}
}
我想按顺序追加所有的arraylist,并希望生成一个arraylist
size = sizeContext*ArraylistOfSingleMember
答案 0 :(得分:11)
您可以使用addAll()方法。
答案 1 :(得分:1)
使用list1.addAll(list2),如果它工作并且更新list1,则返回true。以下是tutorialspoint.com的完整代码示例。
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String args[]) {
// create an empty array list1 with an initial capacity
ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
// use add() method to add elements in the list
arrlist.add(12);
arrlist.add(20);
arrlist.add(45);
// let us print all the elements available in list1
System.out.println("Printing list1:");
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
// create an empty array list2 with an initial capacity
ArrayList<Integer> arrlist2 = new ArrayList<Integer>(5);
// use add() method to add elements in list2
arrlist2.add(25);
arrlist2.add(30);
arrlist2.add(31);
arrlist2.add(35);
// let us print all the elements available in list2
System.out.println("Printing list2:");
for (Integer number : arrlist2) {
System.out.println("Number = " + number);
}
// inserting all elements, list2 will get printed after list1
arrlist.addAll(arrlist2);
System.out.println("Printing all the elements");
// let us print all the elements available in list1
for (Integer number : arrlist) {
System.out.println("Number = " + number);
}
}
}
答案 2 :(得分:0)
for(int i =0 ; i< sizeContext; i++){
contextList.addAll( an instance of ArrayList<Double>);
}