Recently I found two interesting things in JAVA programming. Each of them took me several hours to figure out why.
It is just an address. Any operations actually affect the contents of the object.
For example,
Object O=new Object();
O=(Object) linkedlist.get(i);
O.content1++;
Althoug the object O is a new instance, it has only been cast an memory address of the object i in the LinkedList.
So the content of the object i in the LinkedList has been CHANGED!!! Future operation will have a different LinkedList.
2. How to sort a collection like LinkedList that store any kinds of objects?
I have tried to write a class to solve this problem, since sort would be fundation of many further operations or algrithms like quick search. Later I realized that JAVA standard class has already have a function to do this. An interface called Comparator can tell the how to compare the objects in a collection. So the only thing needed to do is implemented the Comparator with my own way.