Java Reference
In-Depth Information
public void dump() {
for (Node<E> n = head; n != null; n = n.next)
System.out.print(n.value + " ");
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.add("world");
list.add("Hello");
list.dump();
}
}
Solution 89: Generic Drugs
Again, this program appears reasonably straightforward. New elements are added to the head of the
list and the dump method prints the list starting with the head. Therefore, elements are printed in the
opposite order they are added. In this case, the program first adds "world" and then "Hello" , so it
looks as if it is just a convoluted Hello world program. Sadly, if you tried to compile it, you found
that it doesn't compile. The error messages from the compiler are downright baffling:
LinkedList.java:11: incompatible types
found : LinkedList<E>.Node<E>
required: LinkedList<E>.Node<E>
this.next = head;
^
LinkedList.java:12: incompatible types
found : LinkedList<E>.Node<E>
required: LinkedList<E>.Node<E>
 
 
Search WWH ::




Custom Search