mostFrequent方法包adt(抽象数据类型)

时间:2014-02-13 03:12:48

标签: java bag

我正在尝试在包中找到一个名为mostFrequent的方法,该包找到包中最常见的对象例如,如果B = {Bob,Joe,Bob,Ned,Bob,Bob},那么方法 回报鲍勃。提示:方法是O(n ^ 2)。

public E MostFrequent (Bag<E> B){ 
// implementation here
}

行李的内容如下:

package edu.uprm.ece.icom4035.bag;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class StaticBag implements Bag {

    private int currentSize;

    private Object elements[];

    private class BagIterator implements Iterator {

        private int currentPosition;

        public BagIterator(){
            this.currentPosition = 0;
        }

        @Override
        public boolean hasNext() {
            return this.currentPosition < currentSize;
        }

        @Override
        public Object next() {
            if (hasNext()){
                return elements[this.currentPosition++];
            }
            else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }

    public StaticBag(int maxSize){
        if (maxSize < 1){
            throw new IllegalArgumentException("Max size must be at least 1.");
        }

        this.currentSize = 0;
        this.elements = new Object[maxSize];
    }

    @Override
    public void add(Object obj) {
        if (obj == null){
            throw new IllegalArgumentException("Value cannot be null.");
        }
        else if (this.size() == this.elements.length){
            throw new IllegalStateException("Bag is full.");
        }
        else {
            this.elements[this.currentSize++] = obj;
        }
    }

    @Override
    public boolean erase(Object obj) {
        int target = -1;
        for (int i=0; i < this.size(); ++i){
            if (this.elements[i].equals(obj)){
                target = i;
                break;
            }
        }
        if (target == -1){
            return false;
        }
        else {
            this.elements[target] = this.elements[this.currentSize-1];
            this.elements[this.currentSize-1] = null;
            this.currentSize--;
            return true;
        }


    }

    @Override
    public int eraseAll(Object obj) {
        int copies = 0;
        while(this.erase(obj)){
            copies++;
        }

        return copies;
    }

    @Override
    public int count(Object obj) {
        int counter = 0;
        for (int i=0; i < this.size(); ++i){
            if (elements[i].equals(obj)){
                counter++;
            }
        }
        return counter;
    }

    @Override
    public void clear() {
        for (int i=0; i < this.size(); ++i){
            this.elements[i] = null;
        }
        this.currentSize = 0;
    }

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

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

    @Override
    public boolean isMember(Object obj) {
        return this.count(obj) > 0;
    }

    @Override
    public Iterator iterator() {
        return new BagIterator();
    }

}

必须以最有效的方式实施该方法,并且如果可能的话,使用袋子中已经给出的方法来实现

我一直在尝试的是以下内容:

public E MostFrequent(Bag<E> B){
    for(E e : B){
        int counter = B.count(e)

    }
}

但我似乎没有想到在循环中返回具有更多频率的对象的方法

0 个答案:

没有答案