Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Java - Linked List

 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear all,

I done the following LinkedList and Node classes. I have problem coding the sorting portion (the bold line).
I keep getting

cannot find symbol
method compareTo(E)
class: java.lang.object

what should I do to make my coding works??

Thanks in advance

[ September 22, 2005: Message edited by: Timothy Leong ]
 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way,

is Parametric Polymorphism under Java Collections Framework?? Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getItem() method returns an E object, not a LLNode<E> object. The compareTo() method is not available in the E object.

In any case, I wouldn't know how to fix it, as I don't know how the compareTo() method is supposed to work. It looks like all the LLNode<E> class' compareTo() method does is return a -2.

Henry
 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

I have edited my codings

public class BankDatabase extends LinkedList<Customer>
{
.....
}

however I keep getting "Type parameter Customer is not within its bound"

what's the problem here?? thanks
[ September 22, 2005: Message edited by: Timothy Leong ]
 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
up...


any1 know wat's wrong with the codes?? Thank you
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not implementing comparable<T> on the node. I think you should reduce the clutter and get a tiny example working first.
[ September 23, 2005: Message edited by: Rick O'Shay ]
 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried casting it using Comparator but still receive the same error.
Do you mean i should compare with nodes instead of the object?

I am trying to compare the E objects together and let them call their compareTo methods which I have already specified in their own classes



[ September 24, 2005: Message edited by: Timothy Leong ]
[ September 24, 2005: Message edited by: Timothy Leong ]
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Timothy Leong:
hi guys,

I have edited my codings

public class BankDatabase extends LinkedList<Customer>
{
.....
}

however I keep getting "Type parameter Customer is not within its bound"

what's the problem here?? thanks

[ September 22, 2005: Message edited by: Timothy Leong ]


This one is easy
it should be
public class BankDatabase<Customer> extends LinkedList<Customer>

it says type parameter out of bounds because the Customer type is not avaiable to the BankDatabase. What you've done is just made a "raw" type extend a "generic" type while what you intended was "generic" type extending "generic" type. Hope that helps. (I have not looked into your first problem yet)
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Timothy Leong:


[ September 24, 2005: Message edited by: Timothy Leong ]

[ September 24, 2005: Message edited by: Timothy Leong ]


Try this
public <E extends Comparable<E>> void insert(E o)
and get rid of that cast, I think that should do the job
if it doesn;t work, try the case E temp= curr.getItem(); //this should not have any significant effect
and in the worst,
Comparable<?> temp = (Comparable<?> curr.getItem() //though the cast is unecessary.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic