1

I am experiencing a problem with generics in a CircularLinkedList. I have my CircularLinkedList class, a Node class and a Person class. The Node's should contain persons and the CircularLinkedList should contain those nodes. The problem is, that when i try to create my CircularLinkedList in my Test class, i get an error saying:

Bound mismatch: The type Node<Person> is not a valid substitute for the bounded parameter <E extends Comparable<? super E>> of the type CircularLinkedList<E>

Can you take a look at my generics?

CircularLinkedList.java

 package cirkulærliste;

    import java.util.Random;

    public class CircularLinkedList<E extends Comparable<? super E>> {

        private Node<E> next;
        private Node<E> start;
        private int size = 0;

        public CircularLinkedList() {
            setNext(null);
        }

        /**
         * tilføjer personer
         * @param p
         */
        public void addPerson(E e) {
            Node<E> newNode = new Node<E>(e, null);
            Node<E> tempNext = next;

            if(next == null){
                next = newNode;
                next.setNext(next);
            } else if(size > 1){

            }
        }

        /**
         * udskriver personerne i den rækkefølge de står i listen
         */
        public void print() {
            Node<E> tempNode = start;
            while (!tempNode.getNext().equals(start)) {
                System.out.println(tempNode);
                tempNode = tempNode.getNext();
            }
            System.out.println(tempNode);
        }

        /**
         * en tilfældig person i den cirkulæreliste vælges som start i listen
         */
        public void randomStart() {
            int size = 1;
            Node<E> tempNode = next.getNext();
            while (!tempNode.getNext().equals(next.getNext())) {
                tempNode = tempNode.getNext();
                size++;
            }
            Random randomizer = new Random();
            int chosen = randomizer.nextInt(size);
            for (int i = 0; i <= chosen; i++) {
                tempNode = tempNode.getNext();
            }
            start = tempNode;
        }

        /**
         * fjerner den person fra listen der ligger count pladser fra start i listen. Personen der fjernes returneres.
         * @param count
         */
        public Node<E> remove(int count) {
            Node<E> tempNode2;
            for (int i = 1; i < 5; i++) {
                next = next.getNext();
            }
            tempNode2 = next.getNext();
            next.setNext(next.getNext().getNext());
            tempNode2.setNext(null);
            return tempNode2; 
        }


        public void setNext(Node<E> next) {
            this.next = next;
        }


        public Node<E> getNext() {
            return next;
        }


        public void setStart(Node<E> start) {
            this.start = start;
        }


        public Node<E> getStart() {
            return start;
        }
    }

Node.java

package cirkulærliste;

public class Node<E> {

    private E element;
    private Node<E> next;

    public Node(){
        element = null;
        next = null;
    }

    public Node(E element, Node<E> next){
        this.setElement(element);
        this.next = next;
    }

    public E getElement() {
        return element;
    }

    public void setElement(E element) {
        this.element = element;
    }

    public Node<E> getNext() {
        return next;
    }

    public void setNext(Node<E> newNode) {
        this.next = newNode;
    }

    public String toString(){
        return "" + element;
    }

}

Person.java

package cirkulærliste;

public class Person implements Comparable<Person> {

    private String name;
    private int number;

    public Person(String name, int number){
        this.setName(name);
        this.setNumber(number);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString(){
        return "Name: " + name + "  -  " + "Number: " + number;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    @Override
    public int compareTo(Person o) {
        // TODO Auto-generated method stub
        return 0;
    }

}

Test.java

public class Test {

    public static void main(String[] args) {
        CircularLinkedList<Person> list = new CircularLinkedList<Person>();

        Node<Person> n1 = new Node<Person>();
        n1.setElement(new Person("Thomas", 1));
        list.addPerson(n1); // Compile error occurs here
    }

}
millimoose
  • 39,073
  • 9
  • 82
  • 134
Teilmann
  • 2,146
  • 7
  • 28
  • 57
  • What line does the compiler error occur at? – millimoose Jan 16 '12 at 22:03
  • Where's your test class? It is generally a good idea to post the code that is generating the error. – Ted Hopp Jan 16 '12 at 22:04
  • yeah i know, but i am having a problem with the post.. I cant get it to show the test class. Right now it is showing under the Person class block, which is weird – Teilmann Jan 16 '12 at 22:08
  • There's no reason for the type constraint to be `E extends Comparable super E>` – `CircularLinkedList` doesn't compare its elements against one another. Just make the class signature `CircularLinkedList`; with generics, less is more. – millimoose Jan 16 '12 at 22:27

2 Answers2

2

It sounds like you're writing

CircularLinkedList<Node<Person>>

in your Test class; but you actually need to be writing

CircularLinkedList<Person>

Update for updated question: You also need to change this:

Node<Person> n1 = new Node<Person>();
n1.setElement(new Person("Thomas", 1));
list.addPerson(n1); // Compile error occurs here

to this:

list.addPerson(new Person("Thomas", 1));

In general, code that uses CircularLinkedList shouldn't refer to Node; CircularLinkedList uses Node internally, but users of the class don't need to worry about that.

Also, it's kind of odd that your generic CircularLinkedList class has a method named addPerson. Surely it should be called addElement, or just add?

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • if i write it like this: CircularLinkedList list = CircularLinkedList(); it gives me an error at the parenthesis. Syntax error on token "(", Expression expected after this token – Teilmann Jan 16 '12 at 22:05
  • 1
    @ThomasTeilmann: That's because you're missing the keyword `new`. You need to write `CircularLinkedList list = new CircularLinkedList();`. – ruakh Jan 16 '12 at 22:06
  • arg, jebus. Thx dude, ive been programming for 11 hours straight now.. Im soo tired.. Thanks – Teilmann Jan 16 '12 at 22:09
  • Sorry for the inconvenience. It was such a bad mistake, really. – Teilmann Jan 16 '12 at 22:11
  • @ThomasTeilmann: You mean, something other than what I mentioned in my answer? No, nothing else is wrong. The classes that you posted look fine. – ruakh Jan 16 '12 at 22:11
0

Putting your code into an IDE gives me an error here:

Node<Person> n1 = new Node<Person>();
n1.setElement(new Person("Thomas", 1));
list.addPerson(n1);

A method addPerson(E e) on a collection of type CircularLinkedList<Person> expects a Person instance as a parameter. Your implementation of addPerson even creates a new Node instance (as it should), so there's no need to do it in calling code.

This compiles fine:

list.addPerson(new Person("Thomas", 1));
millimoose
  • 39,073
  • 9
  • 82
  • 134
  • yeah, i know.. :) It was just something that were included with a copy paste of a similar test class. Sorry for the confusion. – Teilmann Jan 16 '12 at 22:34
  • @ThomasTeilmann Could you please post the code that causes actual the error you're asking about, post as little of said code as possible, not post some completely unrelated test code, and also point out where the error occurs? – millimoose Jan 16 '12 at 22:37