I am currently writing some code that generates new solutions and replaces old solutions within an Array List.
public static void main(String args[])
{
ArrayList<sequence> solutions = new ArrayList<sequence>();
int a = 10;
sequence alfa = new sequence();
alfa.GenNewSol(a);
sequence A = new sequence();
A.GenNewSol(a);
sequence B = new sequence();
A.GenNewSol(a);
sequence C = new sequence();
A.GenNewSol(a);
solutions.add(A);
solutions.add(B);
solutions.add(C);
for(int i = 0 ;i < 3;i++) // Generate 3 new solutions based on alfa
{
sequence temp = new sequence(alfa);
temp.GenNewSol(a);
solutions.set(i, temp);
System.out.print("Temp:" + i);
System.out.println(Arrays.deepToString(temp.getSequence()));
}
System.out.println(Arrays.deepToString(solutions.get(0).getSequence()));
System.out.println(Arrays.deepToString(solutions.get(1).getSequence()));
System.out.println(Arrays.deepToString(solutions.get(2).getSequence()));
}
sequence is a class that holds a 2d array variable called input, which holds a specific sequence and its attributes. alfa is an instance of the sequence class. So I generate 3 solutions based on alfa by shuffling the sequence around using GenNewSol and get it to print this sequence(one example):
Temp:0[[0.0, 3.0, 8.0, 9.0, 5.0, 2.0, 7.0, 4.0, 6.0, 1.0]
Temp:1[[1.0, 0.0, 8.0, 9.0, 2.0, 5.0, 7.0, 6.0, 3.0, 4.0]
Temp:2[[3.0, 4.0, 5.0, 9.0, 2.0, 8.0, 1.0, 6.0, 7.0, 0.0]
However, after the loop it prints this out :
[[3.0, 4.0, 5.0, 9.0, 2.0, 8.0, 1.0, 6.0, 7.0, 0.0]
[[3.0, 4.0, 5.0, 9.0, 2.0, 8.0, 1.0, 6.0, 7.0, 0.0]
[[3.0, 4.0, 5.0, 9.0, 2.0, 8.0, 1.0, 6.0, 7.0, 0.0]
It appears as if all of the values that have been looped over have been replaced by the last iteration when I want it to show those initial results.
How can I solve this problem? Does this have to do with object references?
public class sequence
{
double[][] input = {{0,1,2,3,4,5,6,7,8,9}, //sequence # (this is the important part, it is how each procedure in the sequence is labeled and this is the array that I examined above
{3,1,1,5,6,6,4,2,6,4}, // direction
{1,0,0,1,0,0,1,1,0,1}, //method
public sequence()
{}
public sequence(sequenceTest toCopy)
{
this.input = toCopy.input;
}
public void GenNewSol(double a)
{
//Method randomly swaps columns based on input a
}
public double[][] getSequence()
{
return input;
}
}