How do I implement generics <E> in a linked List?

Jon Doe

I have been trying to create a linked list that uses generics to return a data type of the user's choosing. The problem is that my method public E get(int sub) is not recognizing my return cursor.contents as a type E generic.

public E get(int sub)
{
    Node cursor = head; //start at the beginning of linked list. 

    for (int c = 1; c <= sub; c++)
    {
        cursor = cursor.next; //move forward by one. 
    }

    return cursor.contents;//return the element that the cursor landed on. 
}


 public class Node <E>
{
        public E contents; 
    @SuppressWarnings("rawtypes")
    public Node next = null; //points to the next node
    //a method has no return type and has the same name as the class
    public Node(E element)
    {
        this.contents = element; 
    }
}

as I have shown above the contents parameter is declared as type E in the Node, but the get method will not recognize cursor.contents as a proper return type.

The system recomments that I either change the return type to Object, which is not an option. Or I change contents to a type E which has already been done, but it still gives me a compilation error.

EpicPandaForce

That's because you need to change it to:

public E get(int sub)
{
    Node<E> cursor = head; //you forgot the generics here

    for (int c = 1; c <= sub; c++)
    {
        cursor = cursor.next; 
    }

    return cursor.contents;
}


 public class Node <E>
{
    public E contents; 
    public Node<E> next = null; //you also even suppressed the raw type here

    public Node(E element)
    {
        this.contents = element; 
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I implement generics <E> in a linked List?

From Dev

How do I implement SelectionSort and InsertionSort on a linked list in Python?

From Dev

How do I implement SelectionSort and InsertionSort on a linked list in Python?

From Dev

How can I implement a linked list in java?

From Dev

How do I Implement the Big Three Correctly: Singly Linked List (C++)

From Dev

How can I implement a len() function for linked list?

From Dev

How do I reverse through a linked list

From Dev

How do I use this Linked List implementation?

From Dev

How do I Sort the names in a Linked List?

From Dev

Rust: How to implement linked list?

From Dev

How to implement an addition method of linked list?

From Dev

How to implement linked list with 1 million nodes?

From Dev

How to implement circular linked list in java?

From Dev

How to implement insert method in a doubly linked list?

From Dev

How to implement erase method in a doubly linked list

From Dev

How do I code a function to test if a linked list is sorted

From Dev

How do I fix that infinite loop on a custom double linked list?

From Dev

How do I get insertion sort to work with a doubly linked list

From Dev

How do I add an object to a linked list in alphabetical order in Java?

From Dev

How do I print the next element in a linked list to a CSV file?

From Dev

How do I remove the first node in a linked list with a remove method

From Dev

How do I properly delete nodes of linked list in C++

From Dev

How do I remove the only node in a linked list in java?

From Dev

How do I get insertion sort to work with a doubly linked list

From Dev

How do I search through a Java Linked List

From Dev

How Do I Fix This Sorted Linked List Insertion?

From Dev

How can I do a recursive print using class linked list

From Dev

How do I properly deallocate memory allocated with a linked list?

From Dev

How do I get the first character of a string from a linked list?

Related Related

  1. 1

    How do I implement generics <E> in a linked List?

  2. 2

    How do I implement SelectionSort and InsertionSort on a linked list in Python?

  3. 3

    How do I implement SelectionSort and InsertionSort on a linked list in Python?

  4. 4

    How can I implement a linked list in java?

  5. 5

    How do I Implement the Big Three Correctly: Singly Linked List (C++)

  6. 6

    How can I implement a len() function for linked list?

  7. 7

    How do I reverse through a linked list

  8. 8

    How do I use this Linked List implementation?

  9. 9

    How do I Sort the names in a Linked List?

  10. 10

    Rust: How to implement linked list?

  11. 11

    How to implement an addition method of linked list?

  12. 12

    How to implement linked list with 1 million nodes?

  13. 13

    How to implement circular linked list in java?

  14. 14

    How to implement insert method in a doubly linked list?

  15. 15

    How to implement erase method in a doubly linked list

  16. 16

    How do I code a function to test if a linked list is sorted

  17. 17

    How do I fix that infinite loop on a custom double linked list?

  18. 18

    How do I get insertion sort to work with a doubly linked list

  19. 19

    How do I add an object to a linked list in alphabetical order in Java?

  20. 20

    How do I print the next element in a linked list to a CSV file?

  21. 21

    How do I remove the first node in a linked list with a remove method

  22. 22

    How do I properly delete nodes of linked list in C++

  23. 23

    How do I remove the only node in a linked list in java?

  24. 24

    How do I get insertion sort to work with a doubly linked list

  25. 25

    How do I search through a Java Linked List

  26. 26

    How Do I Fix This Sorted Linked List Insertion?

  27. 27

    How can I do a recursive print using class linked list

  28. 28

    How do I properly deallocate memory allocated with a linked list?

  29. 29

    How do I get the first character of a string from a linked list?

HotTag

Archive