复制二维ArrayList作为新的 [英] Copy Two Dimensional ArrayList as new

查看:70
本文介绍了复制二维ArrayList作为新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到的问题是复制2d arraylist之后,将元素从一个2d arraylist更改会影响另一个2d arraylist.我希望它们在内存中完全分开.

So the issue I'm having is after copying the 2d arraylist, changing the element from one 2d arraylist affects the other 2d arraylist. I want them to be completely separate in memory.

第一个示例显示了它如何与一维数组列表一起正常工作...

First example shows how it works correctly with 1d arraylists...

import java.util.ArrayList;
public class QuickTest {

    public static void main(String[] args) {
        ArrayList<Integer> firstList = new ArrayList<>();
        ArrayList<Integer> secondList = new ArrayList<>();

        Integer counter = 2;
        for(int arrI = 0; arrI < 4; arrI++, counter+=2){
            firstList.add(counter);
        }

        secondList = new ArrayList<>(firstList);

        System.out.println("firstList.get(2) = " + firstList.get(2));
        System.out.println("secondList.get(2) = " + secondList.get(2));

        firstList.set(2, 7);

        System.out.println("firstList.get(2) = " + firstList.get(2));
        System.out.println("secondList.get(2) = " + secondList.get(2));
    }
}

预期输出:

注意如何更改第一个arraylist元素,但不更改第二个arraylist元素.这是件好事,也是我们想要的.

Notice how the element from the first arraylist is changed but not the second arraylist element is not changed. This is good and what we want.

现在尝试复制2d数组列表...

Now to try and copy the 2d arraylists...

import java.util.ArrayList;
public class QuickTest {

    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> firstTwoDimList = new ArrayList<>();
        ArrayList<ArrayList<Integer>> secondTwoDimList = new ArrayList<>();

        firstTwoDimList.add(new ArrayList<Integer>());
        firstTwoDimList.add(new ArrayList<Integer>());
        firstTwoDimList.add(new ArrayList<Integer>());

        Integer counter = 2;
        for(int arrI = 0; arrI < firstTwoDimList.size(); arrI++, counter+=2){
            firstTwoDimList.get(arrI).add(counter);
            counter+=2;
            firstTwoDimList.get(arrI).add(counter);
        }

        secondTwoDimList = new ArrayList<>(firstTwoDimList);

        System.out.println("firstTwoDimList.get(1).get(0) = " + firstTwoDimList.get(1).get(0));
        System.out.println("secondTwoDimList.get(1).get(0) = " + secondTwoDimList.get(1).get(0));

        firstTwoDimList.get(1).set(0, 7);

        System.out.println("firstTwoDimList.get(1).get(0) = " + firstTwoDimList.get(1).get(0));
        System.out.println("secondTwoDimList.get(1).get(0) = " + secondTwoDimList.get(1).get(0));
    }
}

意外的输出:

任何人都知道造成这种情况的原因是什么,最好的解决方案是什么?

Anyone have any idea what the reason for this is, and what the best solution would be?

推荐答案

根据引用,这就是一维数组列表中的情况:

This is what is happening in the 1D array list case, in terms of references:

这是2D阵列列表情况下发生的情况:

This is what is happening in the 2D array list case:

这意味着当您使用以下方法复制阵列列表时:

This means that when you copy an array list using this:

new ArrayList<>(someOldArrayList)

项目本身不会被复制,只会创建一个新的数组列表对象,并引用旧数组列表中的所有项目.

the items themselves don't get copied, only a new array list object is created, referring to all the items in the old array list.

在第二种情况下,您仅更改数组列表2的项,但是第一列表和第二列表的索引1指向同一数组列表2.

In the second case, you are only changing what array list 2's items are, but index 1 of first list and second list refers to the same array list 2.

要解决此问题,您还需要在第一个列表和第二个列表内复制数组列表.一种方法:

To fix this, you need to copy the array lists inside first list and second list as well. One way to do this:

secondList = new ArrayList<>(firstList.stream().map(x -> new ArrayList<>(x)).collect(Collectors.toList()));

这篇关于复制二维ArrayList作为新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆