首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java实现的Bag类

用Java实现的Bag类
EN

Stack Overflow用户
提问于 2013-02-07 13:15:11
回答 1查看 2.5K关注 0票数 0

我必须使用一个集合(而不是Java的内置Collection)来创建一个Bag类。对于这个问题,我找不出equal()方法。基本上,它需要检查两个包是否相同大小,为它们创建副本,使用联合来连接它们,并在for循环中检查每个包中是否存在当前值;如果是,则删除它们。如果两个袋子都是空的,那么它们是相等的。出于某种原因,代码总是输出false?

我为所有的代码道歉,但很难准确地指出哪些是遗漏的,哪些不是,因为大多数代码都是重合的。

谢谢你的帮助!!

编辑:这也伴随着这个问题:Building a bag class in Java

代码语言:javascript
复制
public class Bag<t> implements Plan<t>{
private final int MAX = 10;
private final int DEFAULT = 6;

private static Random random = new Random();

private t[] content; 
private int count;


//Constructors
public Bag(){
    count = 0;
    content = (t[]) (new Object[DEFAULT]);
}
public Bag(int capacity){
    count = 0;
    if(capacity<MAX)
        content = (t[])(new Object[capacity]);
    else System.out.println("Capacity must be less then 10");
}


//Implemented Methods
public void add(t e) {
    try{
        if(!contains(e) && (!(size() == content.length))){
            content[count] = e;
            count++;
        }
        }catch (ArrayIndexOutOfBoundsException exception){
            System.out.println("Bag is Full");
        }
}


public boolean isEmpty() {

    return count==0;        
}


public boolean contains(t e) {
    Object location = null;

    for(int i=0;i<count;i++)
        if(content[i].equals(e)) location=i;

    return (location!=null);
}


public int size() {

    return count;
}


public void addAll(Bag<t> b) {
    for (int i=0;i<b.size();i++)
        add(b.content[i]);
}


public Bag<t> union(Bag<t> a, Bag<t> b) {
    Bag<t> bigBag = new Bag<t>();

    for(int i=0; i<a.size();i++)
        bigBag.add(a.content[i]);
    for(int k=0; k<b.size();k++)
        bigBag.add(b.content[k]);

    return bigBag;
}


public boolean equals(Bag<t> e) {
    Bag<t> bag1 = new Bag<t>();
    Bag<t> bag2 = new Bag<t>();
    Bag<t> bag3 = new Bag<t>();
    t object;

    if(size() == e.size()){
        bag1.addAll(this);
        bag2.addAll(e);

        bag3.union(bag1, bag2);

        for(int i=0; i<bag3.size();i++){
            object = bag3.content[i];
            if((bag1.contains(object)) &&(bag2.contains(object))){
                bag1.remove(object);
                bag2.remove(object);

            } 

        }
    }


    return (bag1.isEmpty()&&(bag2.isEmpty()));

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-07 14:05:30

问题似乎出在您的union()方法中,或者是您使用它的方式。它实际上返回了一个新的包,而不是向bag3添加包bag1bag2,这是equals()期望它的行为方式。

你了解类方法了吗?这就是union()的真面目。在签名中添加static关键字,如下所示。

代码语言:javascript
复制
public static Bag<t> union(Bag<t> bag1, Bag<t> bag2)

现在,让我们修改equals方法的定义。示例代码中的equals()的签名为

代码语言:javascript
复制
public boolean equals(Bag<t> e)

但这应该是

代码语言:javascript
复制
public boolean equals(Object o)

因此,我们必须首先确保o是一个Bag

代码语言:javascript
复制
public boolean equals(Object o) {
    if (this == obj) {
        return true;
    } else if (obj == null) {
        return false;
    } else if (!(obj instanceof Bag))
        return false;
    }

    Bag<?> other = (Bag<?>) obj;
    if (this.count != other.count) {
        return false;
    }

    Bag<t> bag1 = new Bag<t>();
    Bag<t> bag2 = new Bag<t>();
    bag1.addAll(this);
    bag2.addAll(other);

    // If you don't know about `static` yet, just use this instead of the following I guess 
    //Bag<t> bag3 = new Bag<t>();
    //bag3 = bag3.union(bag1, bag2);
    Bag<t> bag3 = Bag.union(bag1, bag2);

    t object;
    for(int i=0; i<bag3.size();i++) {
        object = bag3.content[i];
        if ((bag1.contains(object)) && (bag2.contains(object))){
            bag1.remove(object);
            bag2.remove(object);
        }
    }

    return (bag1.isEmpty() && (bag2.isEmpty()));
}

修复几个花括号/编译器错误,我认为这可能会起作用。不过,如果没有缺失的remove()方法,我就无法对其进行测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14744056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档