I have the following object :
class Repeat{
private long startIndex;
private long endIndex;
private int length;
private float repetitions;
private float period;
private int errors;
private float percentOverlap;
public void setPercentOverlap(float percentOverlap) {
this.percentOverlap = percentOverlap;
}
public float getPercentOverlap() {
return percentOverlap;
}
.
. other sets gets etc.
.
}
When I set the percentOverlap and add the Repeat to
ArrayList<Repeat> overlaps = new ArrayList<Repeat>();
overlaps.add(repeat);
Then when i dump this collection to a csv file. I m getting 0.0 for some of the values but not all of them. ie. 6.25 becomes 0.0. I even see this on command line.
Here is the console output:
-> before i add
->Start Index: 570433 End Index: 570465 Overlap :100.0
->Start Index: 570433 End Index: 570465 Overlap :6.25
->Start Index: 570433 End Index: 570465 Overlap :0.0
->Start Index: 570470 End Index: 570510 Overlap :85.0
->Start Index: 570470 End Index: 570510 Overlap :100.0
When i iterate the collection, this is what comes out.
Start Index: 570433 End Index: 570465 Overlap :0.0
Start Index: 570433 End Index: 570465 Overlap :0.0
Start Index: 570433 End Index: 570465 Overlap :0.0
Start Index: 570470 End Index: 570510 Overlap :100.0
Start Index: 570470 End Index: 570510 Overlap :100.0
I took out file writing, just printing to console.
Why is this happening?
Everyone is telling you the right answer: you are adding the same object to the collection more than once.
Your outer loop makes a new Repeat repeat1. Your inner loop sets values in this object and adds it to the collection once on each iteration.
Even though you set different values in repeat1 for each inner iteration, it is still the same object.
This is why you are getting the results you show. Your collection looks something like this:
1: First repeat1
2: First repeat1
3: First repeat1
4: Second repeat1
5: Second repeat1
etc.