DESKTOP-O1UAITJ_20200907-史鹏华

一,问题

我的思路是先把要保留的元素通过add方法加在后面然后把前面的不需要的循环remove删除然后出现报错


    @Override
    public boolean retainAll(Collection<?> c) {
        final int oldCounter = counter ;//7
        if( c instanceof Bag ) {
            Bag other = (Bag) c ;
            Object[] otherElements = other.elements;
            
            
            for (int i = 0; i < other.counter ; i++) {
                Object e = otherElements[ i ];
                X b = (X)e;
                if (this.indexOf(e)!=-1) {
                    this.add(b);
                }
                
                }
//          int n = counter-oldCounter;
//          System.out.println(n); //4
            for (int i = 0; i < oldCounter; i++) {
                this.remove(i);
            }
            
        }
        
        return oldCounter !=counter;
    }
    
    at shipenghua.prequel.Bag.remove(Bag.java:101)
    Exception in thread "main" java.lang.IndexOutOfBoundsException: 被删除元素的索引只能是 [     0 , 5]
    

二,吐槽

.....................