Results 1 to 7 of 7
- 03-28-2012, 09:31 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
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(); } }
Thanks in advance,
MoozicFarmLast edited by Norm; 03-28-2012 at 10:03 PM. Reason: added code tags
- 03-28-2012, 09:45 PM #2
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 09:48 PM.
- 03-28-2012, 10:11 PM #3
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.
- 03-28-2012, 11:03 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
- 03-28-2012, 11:05 PM #5
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.
- 03-28-2012, 11:07 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Generic Linked List help. *Cannot convert from Object to type T
I'm still not following you!
- 03-28-2012, 11:09 PM #7
Similar Threads
-
How to access an element of a linked list inside another linked list?
By smtwtfs in forum New To JavaReplies: 4Last Post: 02-21-2011, 10:34 AM -
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-27-2010, 12:15 AM -
Generic linked list
By mina in forum New To JavaReplies: 1Last Post: 03-17-2010, 03:35 AM -
Convert Linked List Object element to String
By CirKuT in forum New To JavaReplies: 2Last Post: 12-13-2008, 06:22 AM -
List views, a type of object
By Leprechaun in forum New To JavaReplies: 2Last Post: 02-06-2008, 04:07 AM
Bookmarks