Writing this program for class that has to output
The following should print a y x b c:
[a y x b c] <- this is what mine prints with error line commented out
---------------------------------------
The following should print Dan Emily Bill Tom Brett Ashley:
[Dan Emily Bill Tom Brett Chris Ashley] <- and here
---------------------------------------
The following should print a b c d e:
[c a b e d] < -- and here
The error message received:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Iterator<java.lang.String> to java.util.Iterator<java.lang.String>
at TestDoublyLinkedList.main(TestDoublyLinkedList.jav a:33)
My code:
import java.util.Iterator ; public class TestDoublyLinkedList { public static void main(String[] args) { DoublyLinkedList<String> L ; Iterator<String> I ; System.out.println("---------------------------------------") ; L = new DoublyLinkedList<String>() ; L.add("a") ; L.add("b") ; L.add("c") ; L.add(1,"x") ; L.add(1,"y") ; L.add(1,"z") ; L.remove(1) ; System.out.println("The following should print a y x b c:") ; System.out.println( L ) ; System.out.println("---------------------------------------") ; L = new DoublyLinkedList<String>() ; L.add("Dan") ; L.add("Emily") ; L.add("Bill") ; L.add("Tom") ; L.add("Brett") ; L.add("Chris") ; L.add("Ashley") ; // THESE TWO LINES ARE GIVING ME THE ERROR... WELL THE FIRST ONE //I = L.getIterator() ; //while ( I.hasNext() ) { if ( I.next().equals("Chris") ) I.remove() ; } System.out.println("The following should print Dan Emily Bill Tom Brett Ashley:") ; System.out.println( L ) ; System.out.println("---------------------------------------") ; L = new DoublyLinkedList<String>() ; L.add("c") ; L.add("a") ; L.add("b") ; L.add("e") ; L.add("d") ; // L.sort() ; System.out.println("The following should print a b c d e:") ; System.out.println( L ) ; } } /// End-of-File
Other class of program
import java.util.NoSuchElementException; public class DoublyLinkedList<T> { private class Node { private T data; private Node next; private Node previous; public Node(T data,Node next, Node previous) { this.data = data; this.next = next; this.previous=previous; } // all unnecessary methods i didn't include because they have no importance.. private T removeItem(Node item) { System.out.println("removing" + item.data); item.next.previous = item.previous; item.previous.next = item.next; size--; return item.data; } public boolean remove(T element) { Node item = find(element); if (item == null) return false; else { removeItem(item); return true; } } private Node find(T element) { Node ptr = head.next; while (ptr != tail) { if (element.equals(ptr.data)) return ptr; ptr = ptr.next; } return null; } // start ITerator class public Iterator<T> getIterator() { return new DoublyLinkedListIterator(); } class DoublyLinkedListIterator implements Iterator<T> { Node location = head; boolean returnedElement = false; public boolean hasNext() { return location.next != tail; } public T next() { if (location.next == tail) throw new NoSuchElementException(); location = location.next; T element = location.data; returnedElement = true; return element; } public void remove() { if (returnedElement == false) throw new NoSuchElementException(); removeItem(location); returnedElement = false; } } }