Copy Two Dimensional ArrayList as new

前端 未结 4 1201
慢半拍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: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(firstTwoDimList.get(index)));
    }
    

提交回复
热议问题