[Java]Adding to an ArrayList within a for loop - all values replaced by each iteration

Solved

Edit: Solved. None of the stuff was static, oh except for the things that were acting like they were static. I'm smart. Thanks for the fast help you guys!

I'm writing a program to analyze gene sequences, and am trying to obtain data consisting of a section of sequence and an associated value from a list of genes in a .csv file(in the format "sequence, value"). I am able get all the necessary values from the file and use them to create a new Gene object which handles them. However, ArrayList.add() appears to be setting all of the values to the latest gene given rather than just adding it. I've found a number of threads on various sites detailing similar problems, but all of them appear to involve assigning values to a static instance of their object rather than one created inside of the loop(which I made sure not to do).

[Gist with relevant classes and test run function] (https://gist.github.com/anonymous/a18fca61949e112213f4). The output should give sequences and values for each item in the input list, but instead gives the same data(from the final input) 3 times.

I've tried instantiating a temporary ArrayList<Gene> before the loop and setting genes to it at the end, I've tried declaring the genes value in a bunch of different places, I've looked and looked and I can't see how this code could have the error the output suggests(as I said, every other reference I can find to this error are repeatedly resetting and referencing the value of an object outside of the loop). I can see the value of a position in the list is changing after each loop, though the reference is staying the same - why would ArrayList.add() be changing the value of the reference for a previously set object in the list? I'm creating thisGene as a new object each loop before adding it - shouldn't it a new instance be created each time?

I'm assuming this is just some java memory handling feature that I'm not aware of - I've always kind of dismissed these things as something I can mostly ignore and figure out as I go along, but I guess the 5+ hours I've spent absolutely stuck on this seemingly simple issue are showing me that's an attitude I can only keep up for so long.

edit:some wording

Posted Jul 25, 2015, 8:03 AM UTC · edited

44
nutrecht•

You should not use static in your gene class. This means that all instances of the class share that var, so you keep overwriting it.

This isn't a memory feature, it's just you not understanding what static means. If you create instances of a class you almost never want to use it. This also has nothing to do with arraylist; it happens when you create a gene.

4
bobinortOP•

Goddamnit, as soon as I posted this I knew it was just gonna end up being something stupid and obvious I missed. I guess I'll blame lack of sleep and the fact that I've done nothing but python recently... no idea how I looked it over so many times without thinking about this. Thanks a lot for helping me out man, appreciate it.

2
nutrecht•

It happens to all of us ;) You're welcome ;)

2
bob809•

The problem is in the gene class - it's member variables are declared static, so there is only one instance of them shared between all gene objects.

The objects in your arrayList are different, but they share the same variables.

Removing the two static keywords should fix it.

2