java-如何连接两个ArrayLists?

我有两个大小相等的ArrayLists。 列表1由10个名称组成,列表2由其电话号码组成。

我想将名称和号码合并为一个ArrayList。该怎么做?

Anirudh asked 2020-02-17T17:43:51Z
7个解决方案
78 votes

您可以使用.addAll()将第二个列表的元素添加到第一个列表:

array1.addAll(array2);

编辑:根据上面的说明(“我希望新的Arraylist中具有名称和编号的单个String。”),您需要循环浏览第一个列表并将第二个列表中的项目追加到它。

像这样:

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
}

如果输入:

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

你会得到:

array3 : ["a 1", "b 2", "c 3"]
Eric answered 2020-02-17T17:44:26Z
17 votes

将一个ArrayList添加到第二个ArrayList中,如下所示:

Arraylist1.addAll(Arraylist2);

编辑:如果要从两个现有的ArrayList创建新的ArrayList,请执行以下操作:

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

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

arraylist3.addAll(Arraylist2); // add Second arraylist
ρяσѕρєя K answered 2020-02-17T17:44:50Z
2 votes
ArrayList<String> resultList = new ArrayList<String>();
resultList.addAll(arrayList1);
resultList.addAll(arrayList2);
Android Killer answered 2020-02-17T17:45:06Z
2 votes

一个ArrayList1添加到数据中,

mArrayList1.add(data);

和Second ArrayList2添加其他数据,

mArrayList2.addAll(mArrayList1);
Najib Ahmed Puthawala answered 2020-02-17T17:45:30Z
2 votes

对于不复制条目的轻量级列表,您可以使用如下方式:

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

这里执行:

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

}
benez answered 2020-02-17T17:45:55Z
2 votes

如果您只想一行而又不想更改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());
Sergey Orlov answered 2020-02-17T17:46:15Z
0 votes
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++]);
}

如您所说,“名称和数字彼此相邻”。

Harry Lee answered 2020-02-17T17:46:35Z
translate from https://stackoverflow.com:/questions/14017779/how-to-concat-two-arraylists