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

查看:35
本文介绍了将二维 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.

现在尝试复制二维数组列表...

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:

这就是二维数组列表案例中发生的事情:

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天全站免登陆