LinkedList insert tied to inserted object
        Posted  
        
            by wrongusername
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by wrongusername
        
        
        
        Published on 2010-05-06T06:53:39Z
        Indexed on 
            2010/05/06
            6:58 UTC
        
        
        Read the original article
        Hit count: 248
        
java
|linked-list
I have code that looks like this:
public class Polynomial {
    List<Term> term = new LinkedList<Term>();
and it seems that whenever I do something like  term.add(anotherTerm), with anotherTerm being... another Term object, it seems anotherTerm is referencing the same thing as what I've just inserted into term so that whenever I try to change anotherTerm, term.get(2) (let's say) get's changed too.
How can I prevent this from happening?
© Stack Overflow or respective owner