Results 1 to 7 of 7
  1. #1
    MoozicFarm is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Generic Linked List help. *Cannot convert from Object to type T

    Java Code:
    public class LinkedList<T> {
    	
    	
    	public class Node<T>
    	{
    		T contents;
    		Node next;
    		
    	}
    	
    	Node head, tail;
    	
    	public LinkedList()
    	{
    		head = new Node();
    		tail = new Node();
    		
    		head.next =tail;
    		tail.next = null;
    	}
    	
    	public void addEnd(T element){
    		
    		Node newNode = new Node();
    		newNode.contents = element;
    		newNode.next = head.next;
    		head.next = newNode;
    	}
    	
    	public void addAt(int i, T element)
    	{
    		Node current = head;
    		while(i>0){
    			current = current.next;
    			i--;
    		}
    		
    		Node newNode = new Node();
    		newNode.contents = element;
    		newNode.next=current.next;
    		current.next = newNode;
    		
    	}
    	
    	public T get(int i)
    	{
    		Node current = head;
    		while(i>0)
    		{
    			current = current.next;
    			i--;
    		}
    		
    		return current.next.contents;
    	}
    	
    	public T removeAndReturn(int i)
    	{
    		Node current = head;
    		while(i>0){
    			current = current.next;
    			i--;
    		}
    		return current.next.contents;
    		current.next = current.next.next;
    	}
    	
    	public String toString()
    	{
    		StringBuilder S = new StringBuilder("");
    		
    		Node current = head;
    		while(current != tail)
    		{
    			S.append(current.contents.toString() + " ");
    			current = current.next;
    		}
    		return S.toString();
    	}
    
    }
    So here is my linked list class. Not sure what the problem is but when i try and return the contents in getAt(int i) and removeAndReturn(int i) it is telling me that I cannot convert from an Object to type T. Not sure if i am just missing some syntax or what but any help would be appreciated. Any other errors you spot feel free to point out.

    Thanks in advance,

    MoozicFarm
    Last edited by Norm; 03-28-2012 at 09:03 PM. Reason: added code tags

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Generic Linked List help. *Cannot convert from Object to type T

    Each element in a LikedList shoud have a reference to the next and to the previous element (except the first and the last element) of type Node. further you should have a other class that have the members first, last current of type Node and a member size of type int. This class shold have a method add(T), getSize() and so on to organize and administer your elements in the list.
    Last edited by j2me64; 03-28-2012 at 08:48 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Generic Linked List help. *Cannot convert from Object to type T

    You need to add lots of <T> to the required place in the code.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    MoozicFarm is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Generic Linked List help. *Cannot convert from Object to type T

    Quote Originally Posted by Norm View Post
    You need to add lots of <T> to the required place in the code.

    Where do these <T>'s go? Can you show me with the removeAndReplace() method? I am having trouble figuring this out and I do not know why.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Generic Linked List help. *Cannot convert from Object to type T

    One spot would be Node definitions.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    MoozicFarm is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Generic Linked List help. *Cannot convert from Object to type T

    I'm still not following you!

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Generic Linked List help. *Cannot convert from Object to type T

    You have define lots of Node variables without using generics. Change those definitions to use generics.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  2. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  3. Generic linked list
    By mina in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 02:35 AM
  4. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 AM
  5. List views, a type of object
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 03:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •