2014-10-05 90 views
0

迭代器我想遍历结构的一个实例方法内我的数据结构的元素。这里是我的数据结构和它的方法代码:无法调用类型T []

import java.util.Iterator; 

public class DropOutStackArray<T> implements DropOutStack<T>{ 

    private static final int CAP = 10; 
    private int bottom, top, size; 
    private T[] cstack; 


    public static void main(String[] args){ 
     //System.out.println(2%10); 
     //int[] a = new int[10]; 
     //System.out.println(a.length); 
    } 

    private class MyIterator implements Iterator<T>{ 

     private int curr = 0; 

     @Override 
     public boolean hasNext() { 
      return this.curr != DropOutStackArray.this.size; 
     } 

     @Override 
     public T next() { 
      if(hasNext()){ 
       return cstack[curr++]; 
      } 
      return null; 
     } 

     public void remove(){ 
      if(curr == 0) 
       return; 
      cstack[--curr] = cstack[--size]; 
      cstack[curr] = null; 
     } 
    } 

    @Override 
    public Iterator<T> iterator() { 
     // TODO Auto-generated method stub 
     return new MyIterator(); 
    } 

    public DropOutStackArray(){ 
     this.cstack = (T[]) new Object[CAP]; 
     this.bottom = 0; this.top = 0; 
     this.size = 0; 

    } 

    public DropOutStackArray(final int INCAP){ 
     this.cstack = (T[]) new Object[INCAP]; 
     this.bottom = 0; this.top = 0; 
     this.size = 0; 
    } 

    @Override 
    public void push(T data) { 
     // TODO Auto-generated method stub 
     if(this.size == this.cstack.length){ 
      this.cstack[bottom] = data; 
      this.bottom = (this.bottom + 1) % this.cstack.length; 
      this.top = (this.top + 1) % this.cstack.length; 

     } 
     this.cstack[this.top] = data; 
     this.top = (this.top + 1) % this.cstack.length; 
     this.size++; 
    } 


    @Override 
    public T pop(){ 
     T popped; 
     if(!isEmpty()){ 
      int length = this.cstack.length; 
      this.top = (this.top + length - 1) % length; 
      popped = this.cstack[this.top]; 
      this.cstack[this.top] = null; 
      this.size--; 
     }else{ 
      throw new StackEmptyException(); 
     } 
     return popped; 
    } 


    @Override 
    public T peek() { 
     // TODO Auto-generated method stub 
     if(isEmpty()){ 
      throw new StackEmptyException(); 
     } 
     T peeked = this.cstack[this.top-1]; 
     return peeked; 
    } 


    @Override 
    public int size() { 
     return this.size; 
    } 


    @Override 
    public boolean isEmpty() { 
     if(this.size == 0){ 
      return true; 
     } 
     return false; 
    } 

    public String toString(){ 
     Iterator<T> itr = this.cstack.iterator(); 
    } 
} 

我的问题是在最后的方法 - toString()。当我尝试创建itr时,会在标题中发布错误。为什么我不能在堆栈上调用迭代器?

+0

我假设数组不提供迭代器。这里有一个类似的问题:http://stackoverflow.com/questions/3912765/iterator-for-array – 2014-10-05 22:43:55

+0

不幸的是我不能使用任何集合接口。我写了自己的迭代器类,我不得不使用它。 – 2014-10-05 22:45:05

回答

2

您的两个方法toString()iterator()都是DropOutStackArray<T>类中的实例方法。

所以,你应该改变以下 -

Iterator<T> itr = this.cstack.iterator(); 

Iterator<T> itr = this.iterator(); 
相关问题