AbstractList的remove方法 参考一下public void remove() {
    if (lastRet == -1)
throw new IllegalStateException();
            checkForComodification();     try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
    cursor--;
lastRet = -1;
expectedModCount = modCount;
    } catch(IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
    }
}

解决方案 »

  1.   

    用ArrayList来实现,不要用数组不是更好吗?
      

  2.   

    我的头是这样的
    class A implements Iteartor{
    ...
    }
    数组里删除元素的方法怎么写啊?
      

  3.   

    可不可以构造一个新的数组,然后把处理后的数组付给原来的string数组?
      

  4.   

    不懂为什么既然用数据实现了还要用Iterator,有点不伦不类的感觉,
    用ArrayList来实现,不要用数组不是更好吗?(转贴)
      

  5.   

    假如原来的数组很大,如果构造一个新的数组,不会很低效吗?
    当然。我在这是问一个人家说比较简单的技术问题,并不是说有了ArrayList之流,就不关注细节。我很弱智,
    我需要帮助。
      

  6.   

    上面那程序,的remove()方法怎么写呢?
      

  7.   

    import java.util.*;class A{
    private String[] string={"aaa","bbb","ccc","ddd","eee"};
    private int count= 4;
    public Iterator iterator(){
    return new My()     ;
    } public static void main(String[] agrs){
           A a = new A();
           Iterator t = a.iterator();
           t.remove();
        }
    class My implements Iterator{
    public boolean hasNext(){
    if(count<string.length)
    return true;
    return false;
    }
    public Object next(){
    return string[count++].toString();
    }
    public void remove(){
       String[] temp = new String[string.length-1];
               System.arraycopy(string,0,temp,0,count) ;
               System.arraycopy(string,count+1,temp,count,temp.length-count) ;
               for(int i = 0;i<temp.length;i++){
               System.out.println(temp[i]);
               }
    }
    }
    }
      

  8.   

    呵呵。是不是有点傻,总之实现起来是这样的。既然你抛弃ArrayList还想任意操纵Array那只有用“土办法了”-------System.arraycopy()。
      

  9.   

    谢谢!我更改后的代码:import java.util.*;class A{
    private  String[] string={"aaa","bbb","ccc","ddd","eee"};
    private int count= 0;
    public Iterator iterator(){
    return new My();
    } public static void main(String[] agrs){
           A a=new A();
           for(int i=0;i<a.string.length;i++){
            System.out.println(a.string[i]);
           }
           
           System.out.println("=============");
           Iterator it=a.iterator();
           while(it.hasNext()){
            System.out.println(it.next()); it.remove();
           }
          
           for(int i=0;i<a.string.length;i++){
            System.out.println(a.string[i]);
           }
        }
    class My implements Iterator{
    public boolean hasNext(){
    if(count<string.length)
    return true;
    return false;
    }
    public Object next(){
    return string[count++].toString();
    }
    public void remove(){
       String[] temp = new String[string.length-1];
               System.arraycopy(string,0,temp,0,count-1) ;
               System.arraycopy(string,count,temp,count-1,string.length-count) ;
       count-=1;
               string=temp;
    }
    }
    }