• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with generics  RSS feed

 
Gabor Bator
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with generics. I know it may not be an intermediate question, but it just seemed too difficult for a beginner one for me

Basically I have a generic class, called SortedList:



I also have another object, which is defined like



(visibility modifiers are removed)

The problem is, how can I define a member in a class, like



Is it possible at all? I succeeded in declaring it like these:


or
, where E is the type parameter of the class.

However, I do not want to use E, and I do not know what kind of Comparable will be used runtime for IndexElement. I just wanted the IndexElement class to be generic so that it can be used in a type-safe way if the type parameter is known at compile time. In my app, it is not known, but I would like to specify that the SortedList contains IndexElements. Is it possible at all? Whatever I try, ends up with an error message, stating "IndexElement<...> is not within its bound"...

Thanks,
Gabor
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about


and


[ May 24, 2006: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even better than extending ArrayList would be to make your sorted List a Decorator and forward all calls to the underlying list.


This way you can use your SortedList with an ArrayList or a LinkedList or any other List that be implemented in the future.
[ May 24, 2006: Message edited by: Garrett Rowe ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!