我必须使用一个集合(而不是Java的内置Collection)来创建一个Bag类。对于这个问题,我找不出equal()方法。基本上,它需要检查两个包是否相同大小,为它们创建副本,使用联合来连接它们,并在for循环中检查每个包中是否存在当前值;如果是,则删除它们。如果两个袋子都是空的,那么它们是相等的。出于某种原因,代码总是输出false?
我为所有的代码道歉,但很难准确地指出哪些是遗漏的,哪些不是,因为大多数代码都是重合的。
谢谢你的帮助!!
编辑:这也伴随着这个问题:Building a bag class in Java
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()));
}发布于 2013-02-07 14:05:30
问题似乎出在您的union()方法中,或者是您使用它的方式。它实际上返回了一个新的包,而不是向bag3添加包bag1和bag2,这是equals()期望它的行为方式。
你了解类方法了吗?这就是union()的真面目。在签名中添加static关键字,如下所示。
public static Bag<t> union(Bag<t> bag1, Bag<t> bag2)现在,让我们修改equals方法的定义。示例代码中的equals()的签名为
public boolean equals(Bag<t> e)但这应该是
public boolean equals(Object o)因此,我们必须首先确保o是一个Bag。
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()方法,我就无法对其进行测试。
https://stackoverflow.com/questions/14744056
复制相似问题