Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:59:38+00:00 2026-05-28T05:59:38+00:00

I am experiencing a problem with generics in a CircularLinkedList. I have my CircularLinkedList

  • 0

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
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:59:38+00:00Added an answer on May 28, 2026 at 5:59 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am experiencing a problem with my circularlinkedlist. it is a list, that is
I have a Windows Server 2003 64-Bit VPS, and I'm experiencing a problem with
I'm new to web-development in ASP, and I'm experiencing a problem where I try
I'm experiencing this weird problem which my scrollbar jumps by itself to somewhere that
I am experiencing a problem with Swing that only occurs when the computer monitor
I'm a bit newbie to Flash, and I'm experiencing strange problem. I have a
I'm starting to learn mod_rewrite and experiencing a problem that I can't solve myself.
I'm no hero with .htaccess, but I'm experiencing a problem that I think might
I have just started experiencing this problem: The site is fine until I add
I'm experiencing problem with Pretty Faces URL mappings. I have one managed bean with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.