Question

I try to make a generic linked list.

The node of this linked list using <T extends Comparable <T>>. But when I use

LList<LList<Integer>> linkedlist = new LList<LList<Integer>>();

to create a new instance, there is an error:

Multiple markers at this line
- Bound mismatch: The type LList<Integer> is not a valid substitute
   for the bounded parameter <T extends Comparable<T>> of the type LList<T>
- Bound mismatch: The type LList<Integer> is not a valid substitute
   for the bounded parameter <T extends Comparable<T>> of the type

How do I fix this?


Node class:

public class Node <T extends Comparable <T>> {

// Members:
public T data;
public Node <T> next;
// Methods:
public Node () {
    data =null;
    next = null;
}
public Node (T data) {
    this.data = data;
    next = null;
}
}

LList class:

public class LList <T extends Comparable <T>> {

// Members:
public Node <T> head;
// Methods:
public LList () {
    head = null;
}

// Add node.
public void addNode (T data) {
    if (head == null) {
        head = new Node <T> (data);
        return;
    }
    Node <T> newNode = new Node <T> (data);
    Node <T> tempNode = head;
    while (tempNode.next != null) tempNode = tempNode.next;
    tempNode.next = newNode;
}

// Show linked list.
public void showLLForInteger () {
    if (head == null) return;
    Node <T> tempNode = head;
    while (tempNode != null) {
        System.out.print(String.format("%-6d", tempNode.data));
        tempNode = tempNode.next;
    }
    System.out.println();
}
}
Was it helpful?

Solution 2

LList<T extends Comparable>

so LList accepts only classes that extends Comparable as type parameter.

LList <LList<Integer>> subArrsList = new LList <LList<Integer>>();

in this statement you are giving LList<Integer> class as type parameter. LList<Integer> does not extend Comparable. Integer extends comparable but you are not using the Integer class as type parameter, You are using LList which does not extends Comparable.

So you are getting an error.

Change your LList class as follows:

public class LList <T extends Comparable <T>> implements Comparable<LList<T>>{
    @Override public int compareTo(LList<T> list) {
        //to do
    }
...// your code
}

OTHER TIPS

  1. Why are you requiring that T extends Comparable<T>? You do not seem to be using that anywhere.

  2. Since you require that T extends Comparable<T>, that means the parameter of the list must be comparable to itself. LList<LList<Integer>> doesn't work because LList<Integer> is not comparable to itself (it does not extend Comparable<LList<Integer>>). Are you sure you don't just want LList<Integer>?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top