Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Generic Java, type not bounded

Dear guys,
I have declared LinkedList<Customer> LL = new LinkList<Customer> (); in my main class.

Under my class Customer, I have a compareTo method too.

I get an error on Type parameter customer is not within its bound.

I believe the problem lies with the bold portion of the code which sorts the node.

Can somebody guide me please? Thank in advance.

interface Comparable<A>
{
public int compareTo(A that);
}

class LinkedList<E extends Comparable<E>>
// Representation of a linked list of objects of type E.
{

private int numItems; // number of elements in list
private LLNode<E> head; // pointer to head of list

public LinkedList()
// Default constructor - create new empty linked list.
{
numItems = 0;
head = null;
}

// Accessor methods.
public LLNode<E> getHead() { return head; }
public int size() { return numItems; }
public boolean isEmpty() { return (head == null); }

public void setHead (LLNode<E> newHead)
{
head = newHead;
}

public void setNumItems (int newNumItems)
{
numItems = newNumItems;
}

public void insert(E var)
{
LLNode<E> newNode = new LLNode<E> (var);
setNumItems(size()+1);
if(isEmpty())
setHead(newNode);
else
{ LLNode<E> prev=null;
for(LLNode<E> curr=getHead(); curr != null; prev = curr, curr=curr.getNext())
{
E tmpItem = curr.getItem();
if(tmpItem.compareTo(var) > 0)
{
// insert before curr node
newNode.setNext (curr);
if (prev != null)
prev.setNext (newNode);
else
setHead (newNode);

return;
}

}
// insert at end of list
prev.setNext (newNode);
}
}


}
[2422 byte] By [synckewl] at [2007-11-11 6:54:17]
# 1 Re: Generic Java, type not bounded
what does your Customer class look like? Does it declare that it implements the Comparable interface?

Have you thought about leaving out your self-written Comparable interface and just used the JDK's Comparable interface?
nspils at 2007-11-11 22:40:01 >