【发布时间】:2016-05-25 03:19:09
【问题描述】:
我正在完成一项学校作业。目标是练习 GUI、clone() 方法以及使用/修改现有代码。 我正在尝试以教师希望的方式编写 equals 方法——通过使用对象的克隆,从包中删除项目(根据删除成功或失败返回布尔值)。 p>
包以数组表示,在{1,2,3}和{3,2,1}等情况下应返回true,即顺序无关紧要,只有每个数字的个数出现在数组。
问题来了
它在大多数情况下都有效,但是在袋子包含以下数字的情况下存在错误:{1,1,2} 和 {1,2,2} 以及其他类似的迭代。它返回的是 true 而不是 false。
我相信这与我们应该使用的 remove() 方法有关。如果我理解正确,它应该将值放在数组的“末尾”并减少 manyItems 计数器(这是数组中项目数的变量,因为 array.length 在构造函数中默认为 10 .)
代码大部分是由另一个人编写的。我们必须导入现有文件并编写新方法来完成我们被赋予的任务。我已经完成了所有的 GUI 部分,所以我不会包含那个类,只包含 IntArrayBag 类中使用的方法。
第二双眼睛会很有帮助。谢谢。
public class IntArrayBag implements Cloneable
{
// Invariant of the IntArrayBag class:
// 1. The number of elements in the bag is in the instance variable
// manyItems, which is no more than data.length.
// 2. For an empty bag, we do not care what is stored in any of data;
// for a non-empty bag, the elements in the bag are stored in data[0]
// through data[manyItems-1], and we don�t care what�s in the
// rest of data.
private int[ ] data;
private int manyItems;
public IntArrayBag( )
{
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = new int[INITIAL_CAPACITY];
}
public IntArrayBag clone( )
{ // Clone an IntArrayBag object.
IntArrayBag answer;
try
{
answer = (IntArrayBag) super.clone( );
}
catch (CloneNotSupportedException e)
{ // This exception should not occur. But if it does, it would probably
// indicate a programming error that made super.clone unavailable.
// The most common error would be forgetting the "Implements Cloneable"
// clause at the start of this class.
throw new RuntimeException
("This class does not implement Cloneable");
}
answer.data = data.clone( );
return answer;
}
public int size( )
{
return manyItems;
}
public boolean remove(int target)
{
int index; // The location of target in the data array.
// First, set index to the location of target in the data array,
// which could be as small as 0 or as large as manyItems-1; If target
// is not in the array, then index will be set equal to manyItems;
for (index = 0; (index < manyItems) && (target != data[index]); index++)
// No work is needed in the body of this for-loop.
;
if (index == manyItems)
// The target was not found, so nothing is removed.
return false;
else
{ // The target was found at data[index].
// So reduce manyItems by 1 and copy the last element onto data[index].
manyItems--;
data[index] = data[manyItems];
return true;
}
}
//I added extra variables that are not needed to try to increase readability,
//as well as when i was trying to debug the code originally
public boolean equals(Object obj){
if (obj instanceof IntArrayBag){
IntArrayBag canidate = (IntArrayBag) obj; // i know this can be changed, this was required
IntArrayBag canidateTest = (IntArrayBag) canidate.clone(); //this was created
//as a clone because it was otherwise referring to the same memory address
//this caused items to be removed from bags when testing for equality
IntArrayBag test = (IntArrayBag) this.clone();
//fast check to see if the two objects have the same number of items,
//if they dont will return false and skip the item by item checking
if (test.size() != canidateTest.size())
return false;
//the loop will go through every element in the test bag it will
//then remove the value that is present at the first index of the test bag
for (int i = 0; (i < (test.size()) || i < (canidateTest.size())); i++){
int check = test.data[i];
//remove() returns a boolean so if the value is not present in each bag
//then the conditional will be met and the method will return false
boolean test1 = test.remove(check);
boolean test2 = canidateTest.remove(check);
if (test1 != test2)
return false;
}//end for loop
// if the loop goes through every element
//and finds every value was true it will return true
return true;
}//end if
else
return false;
}//end equals
}
【问题讨论】:
-
也许我遗漏了一些东西,但我没有在您的帖子中看到具体问题。请明确说明您要问的是什么。
-
为什么要克隆equals中的对象?
-
编辑应该反映您的 cmets。
-
感谢大家尝试提出其他解决问题的方法。非常感谢。不幸的是,当我们第一次得到作业时,我问老师是否可以用另一种方式解决问题。她坚持要我们克隆这些物体。它的编写方式在大多数情况下都有效。我正在调试,并且 remove() 方法出现了问题,有时它无法正常运行(在一种情况下,它将 1 变成了 2)。这个方法不是我写的。我尝试修改该方法,但无济于事,我无法使其按预期工作。
-
坚持使用
clone(),老师有解释吗?它仍然有一些防御者,但clone()和Cloneable是 generally considered broken,你很少会遇到他们的实际用例。
标签: java arrays object clone equals