DESKTOP-7803S27_20200907-吴远亮

问题

如何完善下列三个方法:

public boolean containsAll(Collection<?> c) {

public boolean removeAll(Collection<?> c)

public boolean retainAll(Collection<?> c)

解决方法

containsAll:

public boolean containsAll(Collection<?> c) {
            if(c instanceof Bag) {
                Bag other=(Bag) c ;
                for(int a=0;a<other.counter;a++) {
                X b=(X) other.elements[a];
                //other.elements中 任何一个不包容在 Bag.elements 中都返回false
                    if(indexOf(b)==-1) {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

removeAll

public boolean removeAll(Collection<?> c) {
            int oldlength=counter;
            if(c instanceof Bag) {
                Bag other=(Bag) c;
                for (int a = 0; a < other.counter; a++) {
                X b=(X) other.elements[a];
                remove(b);
                }
            }
            return oldlength!=counter;
        }

retainAll 方法尚未解决

吐槽

今天下午有点热,有点犯困。