3

I am trying to implement a generic DDLinkedList of Entries. I have the following classes defined.

class DoublyLinkedList<T extends Comparable<T>> 
class DLLNode<T extends Comparable<T>> 
Entry<K extends Comparable<K>, V> implements Comparable<Entry<K, V>>

Once I try to create an array of DoublyLinkedlist of type Entry as below:

DoublyLinkedList<DLLNode<Entry<K, V>>> array[] = (DoublyLinkedList<DLLNode<Entry<K, V>>>[]) new DoublyLinkedList[TABLE_SIZE];

I get an error message:

"The type DLLNode<Entry<K,V>> is not a valid substitute for the bounded
parameter <T extends Comparable<T>> of the type DoublyLinkedList<T>"

From my other standing I thought I could make a Generic Type T of Entry.

My question: Am I going about the wrong way of doing this or am I implementing it wrong?

  • http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java – assylias Apr 15 '15 at 20:49
  • Please clarify what you're trying to do exactly. Why are you trying to make an array of a linked list? See [http://stackoverflow.com/help/mcve](http://stackoverflow.com/help/mcve). Try to update the question with a more substantial code example and explanation which more clearly illustrate the problem. As it is, we can't do much more than explain the compiler error. – Radiodef Apr 15 '15 at 20:51

3 Answers3

4

Your definition for DoublyLinkedList says that it must take a type which is comparable with itself. But your definition for DLLNode does not implement Comparable<DLLNode<T>> which means that DLLNode is not comparable with its own type. So you can't use DLLNode as a parameter type in DoublyLinkedList.

Bobulous
  • 12,241
  • 4
  • 36
  • 63
0

DLLNode needs to actually implement Comparable and not use it as its generic type.

Also your syntax is way off. Arrays cannot use generics. The best you can do is this:

DoublyLinkedList<?> array[] = new DoublyLinkedList[TABLE_SIZE];
Necreaux
  • 8,456
  • 7
  • 22
  • 43
  • 2
    `new DoublyLinkedList<>[TABLE_SIZE]` is a compiler error. – Radiodef Apr 15 '15 at 20:48
  • 2
    You simply cannot use generics when initializing an array. – Luiggi Mendoza Apr 15 '15 at 20:48
  • I'll fix my side note about syntax, but my solution I think is still correct. – Necreaux Apr 15 '15 at 20:50
  • 1
    Arrays *can use* generics, they just can't be instantiated generically. The OP was actually doing that part right. ;p – Radiodef Apr 15 '15 at 20:52
  • @Radiodef If they can't be instantiated that way, what's the point of declaring them that way? I personally think it is better to show that is unchecked than pretend it is checked, but there are always cases where you need to hold your nose and do that. – Necreaux Apr 15 '15 at 20:56
  • Strong static checking of what you can put in the array, less casting when you take stuff out of it. – Radiodef Apr 15 '15 at 21:02
  • https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays – mrak Apr 15 '15 at 21:06
0

You need to do:

class DoublyLinkedList<T extends Comparable<? super T>> {}

class DLLNode<T extends Comparable<? super T>> implements Comparable<DLLNode<T>> {
    public int compareTo(DLLNode<T> other) {
        return 0;
    }
}

abstract class Entry<K extends Comparable<? super K>, V> implements Comparable<Entry<K, V>> {}

class HashEntry<K extends Comparable<? super K>, V> extends Entry<K, V> {
    public int compareTo(Entry<K, V> other) {
        return 0;
    }
}

Then wherever you decide to use this:

DoublyLinkedList<DLLNode<HashEntry<K, V>>> array[] = new DoublyLinkedList[TABLE_SIZE];

The reason why this works is because of that <? super T> declared for the DLLNode class. If you just have <T>, this will not work because HashEntry will not be comparable. Generics in Java is a bit of a pain to get right.

smac89
  • 26,360
  • 11
  • 91
  • 124
  • `new DoublyLinkedList<>[TABLE_SIZE]` is a compiler error. – Radiodef Apr 15 '15 at 21:03
  • Thank you. The implements Comparable was definitely one of my problems. However now on K when I try create the array give an error message of `Bound mismatch: The type K is not a valid substitute for the bounded parameter > of the type HashEntry` – user2390775 Apr 15 '15 at 21:18
  • @user2390775 This new compiler error is due to declarations in code you haven't shown us. – Radiodef Apr 15 '15 at 21:23