将三维ArrayList复制为Pass-By-Value

时间:2018-02-09 11:56:55

标签: java arraylist

Java Version 8

IntelliJ IDEA 2016.1.4
Build #IC-145.2070, built on August 2, 2016
JRE: 1.8.0_77-b03 x86
JVM: Java HotSpot(TM) Server VM by Oracle Corporation

我需要能够复制这个ArrayList,以便在对ArrayList中的值进行更改(添加,删除,编辑)时......这些更改必须只在复制的ArrayList中进行,而不会影响原始的ArrayList

以下提供的示例代码:

第61行:是传值的副本应该发生的地方。

第19-23行:我正在考虑创建复制值传递方法,然后只使用第61行上的方法调用。

package Main;
import java.util.ArrayList;

public class QuickTest {
    private static void printHpMpLvlsValues(ArrayList<ArrayList<ArrayList<Integer>>> hpMpLvlsValues, int hpValuesIndex, int mpValuesIndex){
        int currentLvl = 1;
        for (int lvlIndex = 0; lvlIndex < hpMpLvlsValues.get(hpValuesIndex).size(); lvlIndex++, currentLvl++){
            System.out.println("****************** LVL(" + currentLvl + ") Possible Values ******************");
            System.out.println("HP Values = " + hpMpLvlsValues.get(hpValuesIndex).get(lvlIndex));
            System.out.println("MP Values = " + hpMpLvlsValues.get(mpValuesIndex).get(lvlIndex));
            System.out.println();
        }
        System.out.println("*************************************************************");
        System.out.println("*************************************************************");
        System.out.println("*************************************************************");
        System.out.println();
    }

    private static ArrayList<ArrayList<ArrayList<Integer>>> getCopyThreeDimArrListPassByValue(ArrayList<ArrayList<ArrayList<Integer>>> origThreeDimArrList){
        ArrayList<ArrayList<ArrayList<Integer>>> copiedThreeDimArrList = new ArrayList<>();
        //ToDo: fill in code to copy the Three Dimensional ArrayList as pass-by-value.
        return copiedThreeDimArrList;
    }

    public static void main(String[] args) {
        int hpValuesIndex = 0;
        int mpValuesIndex = 1;

        // Setup originalHpMpLvlsValues ArrayList...
        ArrayList<ArrayList<ArrayList<Integer>>> originalHpMpLvlsValues = new ArrayList<>();
        ArrayList<ArrayList<ArrayList<Integer>>> copiedHpMpLvlsValues = new ArrayList<>();
        ArrayList<ArrayList<Integer>> hpLvlsValues = new ArrayList<>();
        ArrayList<ArrayList<Integer>> mpLvlsValues = new ArrayList<>();
        int maxLvl = 10;
        int lvlTotalNumOfValuesPerLvl = 5;
        int currentHpValue = 50;
        int IncreaseHpValue = 50;
        int currentMpValue = 10;
        int IncreaseMpValue = 10;
        for (int lvlIndex = 0; lvlIndex < maxLvl; lvlIndex++){
            hpLvlsValues.add(new ArrayList<>());
            mpLvlsValues.add(new ArrayList<>());
            for (int valueNum = 1; valueNum <= lvlTotalNumOfValuesPerLvl; valueNum++){
                hpLvlsValues.get(lvlIndex).add(currentHpValue);
                mpLvlsValues.get(lvlIndex).add(currentMpValue);
                currentHpValue += IncreaseHpValue;
                currentMpValue += IncreaseMpValue;
            }

        }
        originalHpMpLvlsValues.add(hpLvlsValues);
        originalHpMpLvlsValues.add(mpLvlsValues);
        // End Setup originalHpMpLvlsValues ArrayList...

        // Print multiple hp/mp possible level results to system output
        System.out.println("************************************************************");
        System.out.println("Original HP/MP Levels Results:");
        printHpMpLvlsValues(originalHpMpLvlsValues, hpValuesIndex, mpValuesIndex);

        // Attempt to copy originalHpMpLvlsValues ArrayList as pass-by-value
        copiedHpMpLvlsValues = originalHpMpLvlsValues;                       // <--------- Insert how to copy pass-by-value here

        // Change hpValue for 5th level from 1150 to 1175
        // ... this change should only affect "copiedHpMpLvlsValues" ArrayList and NOT the "originalHpMpLvlsValues" ArrayList
        int lvlFiveIndex = 4;
        copiedHpMpLvlsValues.get(hpValuesIndex).get(lvlFiveIndex).set(2, 1175);

        // Print change made to copiedHpMpLvlsValues ArrayList
        System.out.println("************************************************************");
        System.out.println("Copied HP/MP Levels Results with change(s):");
        printHpMpLvlsValues(copiedHpMpLvlsValues, hpValuesIndex, mpValuesIndex);

        // Print originalHpMpLvlsValues ArrayList to ensure NO changes were made to this ArrayList
        System.out.println("************************************************************");
        System.out.println("Original HP/MP Levels Results without change(s):");
        printHpMpLvlsValues(originalHpMpLvlsValues, hpValuesIndex, mpValuesIndex);
    }
}

作为旁注,我得到了如何将二维ArrayLists复制为值传递值的帮助 - &gt; Copy Two Dimensional ArrayList as new(阅读清洁工在底部用1行代码回答)

1 个答案:

答案 0 :(得分:0)

填写原帖子第19-23行的复制方法......

private static ArrayList<ArrayList<ArrayList<Integer>>> getCopyThreeDimArrListPassByValue(ArrayList<ArrayList<ArrayList<Integer>>> origThreeDimArrList){
    ArrayList<ArrayList<ArrayList<Integer>>> copiedThreeDimArrList = new ArrayList<>();
    ArrayList<ArrayList<Integer>> copiedArr;

    for (ArrayList<ArrayList<Integer>> origArr: origThreeDimArrList){
        copiedArr = new ArrayList<>(origArr.stream().map(x -> new ArrayList<>(x)).collect(Collectors.toList()));
        copiedThreeDimArrList.add(copiedArr);
    }

    return copiedThreeDimArrList;
}

然后我在第61行(现在是67)...

上使用此方法调用
copiedHpMpLvlsValues = getCopyThreeDimArrListPassByValue(originalHpMpLvlsValues);

然而......我想知道是否有更好的方法来做这个或者我可以使用的内置库?