I'am trying to learn the implementation of Linked List class in java. But every time I call the get method, I get the contents of Last Node. I'm not able to figure out why. The code is as follow,
package learningLinkedLists;
import java.util.LinkedList;
public class LinkedLists {
public static void main(String[] args) {
Dummy d = new Dummy(0);
LinkedList<Dummy> ll = new LinkedList<Dummy>();
d.SetData(1);
d.printData();
ll.add(d);
d.SetData(2);
d.printData();
ll.add(d);
d.SetData(3);
ll.add(d);
System.out.println(ll);
System.out.println(ll.get(1).data);
System.out.println(ll.get(0).data);
System.out.println(ll.size());
}
}
The output I'm getting is,
1
2
[learningLinkedLists.Dummy@3b061299,learningLinkedLists.Dummy@3b061299,
learningLinkedLists.Dummy@3b061299]
3
3
3
I want to add some data in a class and create linked list of that class.
Thanks in advance!