Copy Two Dimensional ArrayList as new

前端 未结 4 1180
慢半拍i
慢半拍i 2020-12-21 08:42

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

相关标签:
4条回答
  • 2020-12-21 09:07

    I guess I was looking for something like this...

    import java.util.ArrayList;
    public class QuickTest {
        public static ArrayList<ArrayList<Integer>> getTwoDimArrListCopy(ArrayList<ArrayList<Integer>> original){
            ArrayList<ArrayList<Integer>> copy = new ArrayList<>();
    
            for (ArrayList<Integer> arr: original){
                copy.add(new ArrayList<Integer>(arr));
            }
    
            return copy;
        }
    
        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 = getTwoDimArrListCopy(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));
        }
    }
    

    I was just hoping there was a built in library that would do that getTwoDimArrListCopy() function for me...

    0 讨论(0)
  • 2020-12-21 09:09

    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.

    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()));
    
    0 讨论(0)
  • 2020-12-21 09:13

    The difference between your first and second example is that in the second one you use get(). This get() returns a new variable, so you assign the integers to it and not to the original ArrayList.

    If you want to assign a value:

    firstTwoDimList.set(1, new ArrayList<Integer>(Arrays.asList(0, 7)));
    
    0 讨论(0)
  • 2020-12-21 09:17

    You should iterate through the size of the first dimension of the firstTwoDimArray and add new reference of each second dimension to the secondTwoDimArray. i.e.

    for(int index = 0; index < firstTwoDimList.size(); index++) {
        secondTwoDimList.add(new ArrayList<Integer>(firstTwoDimList.get(index)));
    }
    
    0 讨论(0)
提交回复
热议问题