简体   繁体   English

如何连接两个 ArrayLists?

[英]How to concat two ArrayLists?

I have two ArrayList s of equal size.我有两个大小相同的ArrayList List 1 consists of 10 names and list 2 consists of their phone numbers.列表 1 包含 10 个姓名,列表 2 包含他们的电话号码。

I want to concat the names and number into one ArrayList .我想将名称和号码合并为一个ArrayList How do I do this?我该怎么做呢?

You can use .addAll() to add the elements of the second list to the first: 您可以使用.addAll()将第二个列表的元素添加到第一个列表中:

array1.addAll(array2);

Edit: Based on your clarification above (" i want a single String in the new Arraylist which has both name and number. "), you would want to loop through the first list and append the item from the second list to it. 编辑:根据您的上述说明(“ 我想在新的Arraylist中有一个名称和数字都有一个字符串。 ”),您可能想要遍历第一个列表并将第二个列表中的项目附加到它。

Something like this: 像这样的东西:

int length = array1.size();
if (length != array2.size()) { // Too many names, or too many numbers
    // Fail
}
ArrayList<String> array3 = new ArrayList<String>(length); // Make a new list
for (int i = 0; i < length; i++) { // Loop through every name/phone number combo
    array3.add(array1.get(i) + " " + array2.get(i)); // Concat the two, and add it
}

If you put in: 如果你输入:

array1 : ["a", "b", "c"]
array2 : ["1", "2", "3"]

You will get: 你会得到:

array3 : ["a 1", "b 2", "c 3"]

add one ArrayList to second ArrayList as: 将一个ArrayList添加到第二个ArrayList:

Arraylist1.addAll(Arraylist2);

EDIT : if you want to Create new ArrayList from two existing ArrayList then do as: 编辑:如果你想从两个现有的ArrayList创建新的ArrayList,那么执行:

ArrayList<String> arraylist3=new ArrayList<String>();

arraylist3.addAll(Arraylist1); // add first arraylist

arraylist3.addAll(Arraylist2); // add Second arraylist
ArrayList<String> resultList = new ArrayList<String>();
resultList.addAll(arrayList1);
resultList.addAll(arrayList2);

One ArrayList1 add to data, 一个ArrayList1添加到数据,

mArrayList1.add(data);

and Second ArrayList2 to add other data, 和第二个ArrayList2添加其他数据,

mArrayList2.addAll(mArrayList1);

for a lightweight list that does not copy the entries, you may use sth like this: 对于不复制条目的轻量级列表,您可以使用如下:

List<Object> mergedList = new ConcatList<>(list1, list2);

here the implementation: 这里执行:

public class ConcatList<E> extends AbstractList<E> {

    private final List<E> list1;
    private final List<E> list2;

    public ConcatList(final List<E> list1, final List<E> list2) {
        this.list1 = list1;
        this.list2 = list2;
    }

    @Override
    public E get(final int index) {
        return getList(index).get(getListIndex(index));
    }

    @Override
    public E set(final int index, final E element) {
        return getList(index).set(getListIndex(index), element);
    }

    @Override
    public void add(final int index, final E element) {
        getList(index).add(getListIndex(index), element);
    }

    @Override
    public E remove(final int index) {
        return getList(index).remove(getListIndex(index));
    }

    @Override
    public int size() {
        return list1.size() + list2.size();
    }

    @Override
    public void clear() {
        list1.clear();
        list2.clear();
    }

    private int getListIndex(final int index) {
        final int size1 = list1.size();
        return index >= size1 ? index - size1 : index;
    }

    private List<E> getList(final int index) {
        return index >= list1.size() ? list2 : list1;
    }

}

If you want to do it one line and you do not want to change list1 or list2 you can do it using stream 如果你想一行,你不想改变list1或list2,你可以使用流来做

List<String> list1 = Arrays.asList("London", "Paris");
List<String> list2 = Arrays.asList("Moscow", "Tver");

List<String> list = Stream.concat(list1.stream(),list2.stream()).collect(Collectors.toList());
var arr3 = new arraylist();
for(int i=0, j=0, k=0; i<arr1.size()+arr2.size(); i++){
    if(i&1)
        arr3.add(arr1[j++]);
    else
        arr3.add(arr2[k++]);
}

as you say, "the names and numbers beside each other". 正如你所说,“彼此旁边的名字和数字”。

1:you can use addAll() 1:你可以使用 addAll()

List1.addAll(List2);

2: you can use loop: 2:你可以使用循环:

for (String i :List2){
        List1.add(i);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM