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)