i have been trying create linked list uses generics return data type of user's choosing. problem method public e get(int sub)
not recognizing return cursor.contents
type e generic
.
public e get(int sub) { node cursor = head; //start @ beginning of linked list. (int c = 1; c <= sub; c++) { cursor = cursor.next; //move forward one. } return cursor.contents;//return element cursor landed on. } public class node <e> { public e contents; @suppresswarnings("rawtypes") public node next = null; //points next node //a method has no return type , has same name class public node(e element) { this.contents = element; } }
as have shown above contents parameter declared type e
in node, method not recognize cursor.contents
proper return type.
the system recomments either change return type object
, not option. or change contents type e
has been done, still gives me compilation error.
that's because need change to:
public e get(int sub) { node<e> cursor = head; //you forgot generics here (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 suppressed raw type here public node(e element) { this.contents = element; } }
Comments
Post a Comment