DESKTOP-94E2ELT_20200907-刘雪丽

一、问题

老师说的好,就是一点一点加进去的,有点杂,不能说一下就看懂,练习的过程也很大程度上依靠老师发的代码去练习的。

1、public boolean contains(Object o) {
        for(int i=0;i<count;i++) {
            if(array[i].equals(o)) {
                return true;
            }
        }
        return false;
    }

2、public boolean remove(Object o) {
        for(int i=0;i<count;i++) {
            if(array[i].equals(o)) {
                System.arraycopy(array, i+1, array, i, count-i);
                count--;
                return true;
            }
        }
        return false;
    }


3、public boolean containsAll(Collection<?> c) {
        if(c  instanceof Bag ) {
            Bag b=(Bag) c;
            Object ob[]=b.toArray();
            for(int x=0;x<ob.length;x++) {
                this.contains(ob[x]);   
            }
            return true;
        }
        return false;
}

4、public boolean addAll(Collection<? extends X> c) {
        final int oldl=count;
        if(c instanceof Bag) {
            Bag other=(Bag)c;
            X  x=(X)other.toArray();
           this.add(x);
        }
        return oldl!=count;
    }

    @Override
5、public boolean removeAll(Collection<?> c) {
        int oldsize=count;  
        if(c instanceof Bag) {
            Bag b=(Bag)c;
        }   
        for(int j=0;j<c.size();j++) {
            if(this.contains(c.toArray()[j])) {
                this.remove(c.toArray()[j]);
                count--;
            }
        }
        return oldsize!=count;
    }

    @Override
6、public boolean retainAll(Collection<?> c) {

        if(c instanceof Bag) {
            Bag b=(Bag)c;
            Object newarr[]=b.toArray();
        for(int j=0;j<count;j++) {
            if(this.contains(newarr[j])==false) {
               this.remove(newarr[j]);
                count--;
            }
        }}
        return false;
    }

比较难理解的是在传的参数上,可能有点混乱,后边这几个都是接口实现类的对象,我在测试的时候忘了就直接传参数,有点搞混了,对象可以直接声明引用变量(给值后)并用它进行操作,这里就不需要参数了。

在类型强制转换的时候出了点问题,等明天早上来了问老师,方便点。