-2

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

}
  • 2
    Post `sequence`. Guessing that everything is `static`. – Elliott Frisch Jul 15 '18 at 21:24
  • 3
    I can think of half a dozen reasons why this might happen. You've only shown us a tiny portion of your code, and I can only guess at what the rest of it might do. Can you please create a minimal, complete, verifiable example, so that we've got enough code to actually reproduce your problem? – Dawood ibn Kareem Jul 15 '18 at 21:26
  • To add to the above comments: please first spend some time forming an [mcve]. This is usually step 1 when asking a question on Stackoverflow, and not infrequently leads to people discovering the problem all on their own, before actually hitting "post", because reducing the problem makes it super obvious _why_ things are broken. Sometimes, it doesn't, in which case you have the kind of code you should be putting in your question for others to reproduce and analyse your problem. – Mike 'Pomax' Kamermans Jul 16 '18 at 00:38

1 Answers1

1

Your problem is this line in your sequence class: this.input = toCopy.input;

You're reusing the array reference instead of copying it. See this question for some solutions to copy the array -- Arrays.copyOf() or System.arrayCopy()

side nit: please follow conventions when coding such as capitalizing the first letter of class names; it makes it easier for others to follow

Krease
  • 15,805
  • 8
  • 54
  • 86