0

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

I need to be able to copy this ArrayList for when changes are made to values in the ArrayList (added, removed, edited)... And these changes made, must only be made in the copied ArrayList without Affecting the original ArrayList.

Example Code Provided Below:

Line 61: is where the copy of pass-by-value should happen.

Lines 19-23: is where I was thinking of creating the copy pass-by-value method, and then just using the method call on line#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);
    }
}

As a side-note, I got help with how to copy two dimensional ArrayLists as pass-by-value here -> Copy Two Dimensional ArrayList as new (read sweepers answer at the bottom done with 1 line of code)

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • 1
    line 19-23, line 61. Hard to spot it without copying pasting your code. – davidxxx Feb 09 '18 at 12:01
  • I think what you are looking for is to deep copy a Java List: https://stackoverflow.com/a/715660/5165833 – arxakoulini Feb 09 '18 at 12:01
  • 1
    `ArrayList>>` Ouh ! – davidxxx Feb 09 '18 at 12:02
  • Yup... some work needs to be done... I will post what I have so far as an answer (working), but I wonder if there is a better way... – Fiddle Freak Feb 09 '18 at 12:04
  • You're making an `ArrayList` within an `ArrayList` within an `ArrayList`. You're sure you're not working on the source code of Inception? – MC Emperor Feb 09 '18 at 12:05
  • your code is so confusing! what exactly you wanna do with this program? The implementation could be better, i guess! – HMD Feb 09 '18 at 12:06
  • I'm writing a calculator to predict ranged levels values based upon values and random numbers using factorial numbers and many conditional boundries... so yes, it's complicated. This specific problem has been worded well enough, and the purpose is just to test the three dimensional array while making changes to the array, but not affecting the original array. – Fiddle Freak Feb 09 '18 at 12:09
  • @FiddleFreak you can create a class for the copied `ArrayList` and give the original one to its constructor. Then use nested loops (Iteration) to manually assign values of original list to the copied one. – HMD Feb 09 '18 at 12:16
  • @HMD This is sort of what I did... As of now in my answer below, I'm using this method in a class called supportMethods... and I just import that class and use this method. But again... I really hate digging into this low level copying, when I would think there is an easier way... – Fiddle Freak Feb 09 '18 at 12:23

1 Answers1

1

Filled in the copy method on lines 19-23 in original post...

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;
}

Then I just use this method call on line#61(now 67)...

copiedHpMpLvlsValues = getCopyThreeDimArrListPassByValue(originalHpMpLvlsValues);

However... I was wondering if there was a better way to do this or a built-in library I could use for this??

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83