I have a small program with an algorithm to iterate through n number of parcels to find a certain weight numWeight. After each iteration, the weight of a parcel is increased. When a solution is found, the solution should be stored in the solutions variable. As such, when a solution is found, (when currentWeight == numWeight) I want the current instance of comb to be stored in an index of the solutions arraylist. However, when try and store comb in solutions, the instance stored continues to change with comb. How would I get it so that solutions could store an instance of comb as it is at the time that line executes? The code snippet in question is as follows:
public void solution2(){
BookCombination comb = new BookCombination(numParcels, mailClass);
ArrayList<BookCombination> solutions = new ArrayList<>();
int currentWeight;
while (!stopCalc){
currentWeight = comb.getWeight();
if (currentWeight == 0){
break;
} else if (currentWeight == numWeight){
solutions.add(comb);
comb.increaseWeight();
} else {
comb.increaseWeight();
}
}
}
Thanks!