2012-04-11 61 views
0

我這個簡單的代碼迭代器仿製藥,不能宣佈不匹配的類型(也許鑄造發行)的迭代器

public String toString() { 

    **Iterator it = list.iterator();** 
    String s; 

    while(it.hasNext()){ 

    s = s + " " + Integer.toString(it.next()); 

    } 
    System.out.println(s); 

// IMPLEMENT THIS METHOD VIA AN ITERATOR 

// MAKE SURE THE OUTPUT PRODUCED BY THE METHOD 
// DISPLAYS THE LEAST SIGNIFICANT BIT TO THE RIGHT. 

    } 

在用星號線,我得到以下編譯錯誤。

Error: incompatible types 
    required: Iterator 
    found: java.util.Iterator<java.lang.Integer> 

我已經試過了,

**Iterator<Integer> it = list.iterator();** 

我得到的,Error: type Iterator does not take parameters

編輯*

我忘了提,我有我自己的實現的接口方法。

/* 
    * Inner class to implement the iterator for the <code>BitList</code>. 
    */ 
    private class BitListIterator implements Iterator { 

    /* 
    * A reference to the current <code>Node</code> in the iterator. 
    */ 
    private Node current = null; 

    /* 
    * The expected modification count variable. 
    * Needed for the "fast-fail" implementation of the iterator. 
    */  
    private int expectedModCount = modCount; 

    /* 
    * Advances the iterator to the next element in the list. 
    */ 
    public int next() { 
     checkValid(); 

     if (current == null) { 
     current = first ; 
     } else { 
     current = current.next ; // move the cursor forward 
     } 

     if (current == null) 
     throw new NoSuchElementException() ; 

     return current.value ; 
    } 

    /** 
    * Inserts a new <code>int</code> value immediately before the next element that would be returned by <code>next()</code>. 
    */  
    public void add(int newElement) { 
     Node newNode; 

     if (current == null) { 
     first = new Node(newElement, first); 
     current = first; 
     } else { 
     current.next = new Node(newElement, current.next); 
     current = current.next; 
     } 

     modCount++ ; 
     expectedModCount++ ; 
    } 

    /** 
    * Indicates whether there is a next element in the list being traversed or not. 
    */  
    public boolean hasNext() { 
     return ((current == null) && (first != null)) || 
     ((current != null) && (current.next !=null)); 
    } 

    /** 
    * Checks whether this iterator remains valid, i.e. no concurrent modifications have been made on the list. 
    */  
    private void checkValid() { 
     if (expectedModCount != modCount) 
     throw new ConcurrentModificationException(); 
    } 
    } // end of BitListIterator class 

因此,如果導入的包我收到以下錯誤

Error: BitList.BitListIterator is not abstract and does not override abstract method remove() in java.util.Iterator 

Error: next() in BitList.BitListIterator cannot implement next() in java.util.Iterator 
    return type int is not compatible with java.lang.Object 

我有JDK 1.7和它的使用。

任何想法肯定會有所幫助。

謝謝

Mjall2

+0

編譯器是正確的。你還沒有實現'remove()',你的next()的定義與'Iterator'接口中定義的'next()'不兼容。如果您要使用迭代器類,則需要解決這些問題。 – QuantumMechanic 2012-04-11 00:47:12

+0

「list」是什麼類型?它是'BitList'嗎?當你調用'iterator()'時,你想讓你的'BitList'類返回你的'BitListIterator'實現嗎? – QuantumMechanic 2012-04-11 00:49:35

+0

確保您沒有在同一目錄中名爲Iterator.class的類文件 – newacct 2012-04-11 02:05:56

回答

1

我懷疑你沒有聲明list的類型。試試這個:

private List<Integer> list = new ArrayList<Integer>(); 

此外,該消息表明您已經導入了錯誤Iterator類,它可以使用IDE時發生,如果你不小心。確保import java.util.Iterator;,並且沒有其他Iterator類的導入出現在您的java文件的頂部。

+0

儘管代碼並不明確,但我認爲'list'應該是(已經看不見的)'BitList'類的一個實例,它的存在被添加的'BitList.BitListIterator'類的存在所暗示到原來的問題。 – QuantumMechanic 2012-04-11 00:50:47

2

我懷疑你有沒有在你的文件的頂部得到import java.util.Iterator;

+0

請檢查編輯的文章,謝謝。 – Mjall2 2012-04-11 00:45:38

+0

@ Mjall2:這看起來像一個完全不相關的問題。你的第一個代碼片段不使用「BitListIterator」。 – 2012-04-11 00:46:17

+0

@ Mjall2:你在第一個代碼片段中有「'import java.util.Iterator;'」嗎? – 2012-04-11 00:48:48