[英]Bag Class Implemented in Java
我必须使用一个集合(而不是Java的内置Collection)来创建Bag类。 我为此很难找出equal()方法。 基本上,它需要检查两个袋子的大小是否相同,为它们创建副本,使用联合将它们加入,并在for循环中检查每个袋子中是否包含当前值; 如果是这样,请将其删除。 如果两个袋子都空了,那么它们相等。 由于某种原因,代码会不断吐出假?
对于所有代码,我都表示歉意,但是由于大多数代码是重合的,因此很难指出应该遗漏的内容。
感谢所有的帮助!
编辑:这也伴随着这个问题: 在Java中构建一个bag类
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()));
}
问题似乎出在您的union()方法中,或者与您的使用方式有关。 它实际上返回的是新Bag,而不是将bag1
和bag2
添加到bag3
,这是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()
方法来测试它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.