Java 自制堆栈相等法

Java 自制堆栈相等法,java,stack,Java,Stack,对于我的data structures类,我们必须创建自己的堆栈数据类型以及作为项目的实现。我遇到的问题是当教授要求我们实现一个equals(Object)方法时。这是我到目前为止所拥有的 package stack; import list.*; public class Stack <E> implements StackADT<E>//the interface { List <E> values; public Stack()

对于我的data structures类,我们必须创建自己的堆栈数据类型以及作为项目的实现。我遇到的问题是当教授要求我们实现一个equals(Object)方法时。这是我到目前为止所拥有的

package stack;
import list.*;

public class Stack <E>
implements StackADT<E>//the interface
{
    List <E> values;

    public Stack()
    {
        values = new ArrayList<E>();
    }

    public E push(E value)
    {
        values.add(value);
        return value;
    }

    public E pop()
    {
        return values.remove(values.size()-1);
    }

    public E peek()
    {
        return values.get(values.size()-1);
    }

    /** @return true only if this Stack is empty */
    public boolean isEmpty()
    {
        return (values.size()==0);
    }

    /** Clear this stack, to make it an empty stack */

    public void clear()
    {
        for (int i = 0; i < values.size()-1; i++)
        {
            pop();
        }
    }

    public String toString()
    {
        String result = "[";
        for (int i = 0; i<values.size(); i++)
        { 
            if (i == values.size()-1)
            {
                result = result + values.get(i); 
            }
            else
            {
                result = result + values.get(i) +",";
            }
        }

        result = result + "]";
        return result;
    }

    public boolean equals (Object object)
    {

        if (!(object instanceof StackADT))
        {
            return false;
        }
        StackADT <E> otherStack = new Stack<E>(); 
        for(Object o: object)//heres where i run into trouble
        {
            otherStack.push(o);
        }
        for (int i=0;i<values.size()-1;i++)
        {
            if (!(values.get(i).equals(otherStack.pop())))
            {
                return false;
            }
        }
        return true; 
    }

}
包栈;
进口清单*;
公共类堆栈
实现StackADT//接口
{
列表值;
公共堆栈()
{
值=新的ArrayList();
}
公共E推送(E值)
{
增加(价值);
返回值;
}
公共E-pop()
{
返回值.remove(values.size()-1);
}
公共E peek()
{
返回values.get(values.size()-1);
}
/**@仅当此堆栈为空时返回true*/
公共布尔值为空()
{
返回值(value.size()=0);
}
/**清除此堆栈,使其成为空堆栈*/
公共空间清除()
{
对于(int i=0;i对于(int i=0;ia
equals
方法不应该创建任何东西,甚至不应该创建临时对象。与其创建新的
otherStack
,不如将选中的对象强制转换为
StackADT
,如下所示:

// This should be the first line of any equals() implementation:
if (object == this) {
    return true;
}
// You've got this part right: you need to check the other object's type
if (!(object instanceof StackADT)) {
    return false;
}
// Now that you know the type, cast the other object to StackADT<E>
StackADT<E> otherStack = (StackADT<E>)object;
// The next step is to check the sizes:
if (values.size() != otherStack.values.size()) {
    return false;
}
// Finally, go through the individual elements in a loop
//这应该是任何equals()实现的第一行:
if(object==this){
返回true;
}
//这部分你做对了:你需要检查另一个对象的类型
if(!(StackADT的对象实例)){
返回false;
}
//现在您知道了类型,将另一个对象强制转换为StackADT
StackADT otherStack=(StackADT)对象;
//下一步是检查尺寸:
if(values.size()!=otherStack.values.size()){
返回false;
}
//最后,在循环中遍历各个元素
在接下来的循环中,不要弹出另一个堆栈。不要做任何可以修改它的事情。只需检查底层存储(即
),然后逐个检查元素


不要忘了重写
hashCode
:每次重写
equals
时都需要这样做,对象才能履行
java.lang.object

指定的契约堆栈的内部实现是您的列表
,所以为什么不定义两个堆栈的相等值呢
values
?唯一让我困惑的是otherStack.values.size();。otherStack是通过值调用size()吗?@SteakStyles所有这些做的就是检查另一个堆栈的元素数是否与这个堆栈的元素数完全相同。Hi@SergeyKalinichenko重写hashCode也是为了防止调用Object.equals()??@KYLu您重写
hashCode
以遵守
java.lang.Object
文档中概述的契约。性能考虑是次要的,如果它们甚至进入到图片中。我明白了!重写
hashCode()
是为了防止同一哈希集中存在“两个相等的自定义对象”。
// This should be the first line of any equals() implementation:
if (object == this) {
    return true;
}
// You've got this part right: you need to check the other object's type
if (!(object instanceof StackADT)) {
    return false;
}
// Now that you know the type, cast the other object to StackADT<E>
StackADT<E> otherStack = (StackADT<E>)object;
// The next step is to check the sizes:
if (values.size() != otherStack.values.size()) {
    return false;
}
// Finally, go through the individual elements in a loop