2010-12-09 82 views
1

我收到错误“Iterator无法解析为类型”。我试图获取存储类并添加实现java的Collections类所需的代码。 我不认为我被允许导入迭代器,我想我需要做我自己的。无法为集合类创建自己的迭代器

public class storage { 
    private Object[] data = new Object[256]; 
    // Don't allow access to anything not yet stored 
    private int nextEmptySlot = 0; 
    private int i=0; 
    public Object begin(){ 
     return data[0]; 
    } 
    public Object end(){ 
     return data[nextEmptySlot]; 
    } 

    //class Iterator() { 
     // public Storage data; 
    //} 

    public Iterator iterator() { 

     // returns a class that iterates over the data array 
     return new Iterator() { 
      public Object remove(){ 
       for(int j=i+1 ; j<=nextEmptySlot-1 ; j++) { 
        this.data[j-1] = this.data[j]; 
       } 
       return (this.data.data[i]); 
      } 

      public int hasNext(){ 
       if(this.data.data[i+1] != null) return 1; 
       else return 0; 
      } 

      public Object next(){ 
       i++; 
       if (hasNext()==1){ 
        return this.data.data[i]; 
       } 
       else if (hasNext()==0){ 
        throw UnsupportedOperationException();  
       } 
       return this; 
      } 
     }; 
    } 
} 
+1

在一般实践中,类名称以Captial字母开头...`Storage` – 2010-12-09 03:06:11

回答

4

您需要import java.util.Iterator;