How to CompareTo two Object without known about their real type
Posted
by Kamil
on Stack Overflow
See other posts from Stack Overflow
or by Kamil
Published on 2010-03-21T11:52:23Z
Indexed on
2010/03/21
12:01 UTC
Read the original article
Hit count: 497
I have to implement a one linked list but it should put object in appropriate position. Everything was OK when I use it in conjunction with specific class, but when I tried make it universal and argument of method insert was Object some problem appeared. When I want to input Object in right position I should use CompareTo method, but there isn't method in Object class! The problem is how to compare two object elements without known about their real types. Maybe I should use generic class type? But what about CompareTo? Or maybe combine with Element class and place CompareTo there? I suppose it is feasible. :)
public void insert(Object o)
{
Element el = new Element(o);
// initializing and setting iterators
while(!it.isDone() && ((it.current().getValue())).CompareTo(o)<0)
// it.current() returns Element of List
{
//move interators
}
//...
}
© Stack Overflow or respective owner