Java 方法检查两个堆栈是否相等

Java 方法检查两个堆栈是否相等,java,stack,Java,Stack,给出了ArrayStack的以下类定义: Public class ArrayStack<T> implements Stack { T[] stack; int topIndex = -1; 公共类ArrayStack实现堆栈{ T[]堆栈; int topIndex=-1; 在类ArrayStack中编写一个方法equals(Stack other),该方法将堆栈作为参数,如果两个堆栈相等,则返回true,否则返回false public boole

给出了
ArrayStack
的以下类定义:

Public class ArrayStack<T> implements Stack {

    T[] stack;
    int topIndex = -1;
公共类ArrayStack实现堆栈{
T[]堆栈;
int topIndex=-1;
在类ArrayStack中编写一个方法equals(Stack other),该方法将堆栈作为参数,如果两个堆栈相等,则返回true,否则返回false

    public boolean equals(Stack<T> other) {
public boolean equals(堆栈其他){
ArrayStack.java的代码

    import java.util.Arrays;
    import java.util.EmptyStackException;

    public class ArrayStack<T> implements Stacks<T> {

      T[] stack;
      int topIndex = -1;
      private final static int DEFCAP = 100;

      public ArrayStack(int maxSize) {
        stack = (T[]) new Object[maxSize];
      }

      public ArrayStack() {
        this(DEFCAP);
      }

      @Override
      public void push(T element) {
        if (topIndex == stack.length - 1) {
          enlarge();
        }
        topIndex++;
        stack[topIndex] = element;
      }

      @Override
      public T pop() {
        return stack[topIndex--];
      }

      @Override
      public boolean isEmpty() {
        return topIndex == -1;
      }

      @Override
      public  T peak() {
        if (!isEmpty()) {
          return stack[topIndex];
        } else {
          throw new EmptyStackException();
        }
      }

      private void enlarge() {
        stack = Arrays.copyOf(stack, stack.length + DEFCAP);
      }
    }
导入java.util.array;
导入java.util.EmptyStackException;
公共类ArrayStack实现堆栈{
T[]堆栈;
int topIndex=-1;
专用最终静态int DEFCAP=100;
公共阵列堆栈(int maxSize){
堆栈=(T[])新对象[maxSize];
}
公共阵列堆栈(){
这(DEFCAP);
}
@凌驾
公共无效推送(T元素){
if(topIndex==stack.length-1){
放大();
}
topIndex++;
堆栈[topIndex]=元素;
}
@凌驾
公共广播电台{
返回堆栈[topIndex--];
}
@凌驾
公共布尔值为空(){
返回topIndex==-1;
}
@凌驾
公共交通尖峰{
如果(!isEmpty()){
返回堆栈[topIndex];
}否则{
抛出新的EmptyStackException();
}
}
私人有限公司(){
stack=Arrays.copyOf(stack,stack.length+DEFCAP);
}
}
我的尝试:我真的很生气我的尝试有多糟糕,但我现在太封闭了,我不能正确地思考。在思考这个问题时需要你的帮助

public boolean equals(Stack<T> other) {
    if(! other.isEmpty() ) {
        for(int i=0; i < stack.length; i++) {
            if(stack[i].equals(Other.stack[i]) ) {
                return true;
            }
        }
    }

    return false;
}
public boolean equals(堆栈其他){
如果(!other.isEmpty()){
for(int i=0;i

谢谢!

您的equals实现显然是错误的。如果只有一个元素匹配,则返回true。这意味着,只有当所有条目一一不匹配时,才会返回false

如果有任何对应的元素不匹配,则返回false,并在运行循环结束时返回true


如果两个堆栈的大小不同,则还必须返回false。

您的equals实现明显错误。如果只有一个元素匹配,则返回true。这意味着,只有当所有条目一对一不匹配时,才会返回false

public boolean equals(Stack<T> other) {
    //If they point to the same object return true
    if (stack == other) return true;
    //Check for nulls
    if (stack == null || other == null) return false;
    //If the stacks are not the same length, then they won't be equal, easy first test case
    if (stack.length != other.size()) return false;

    for(int i=0; i < stack.length; i++) {
           //Step through each item in both stacks, if any don't match return false
           if(!stack[i].equals(other.stack[i]) ) {
                  return false;
           }
    }

    //Haven't returned yet, they must be equal
    return true;
}
如果有任何对应的元素不匹配,则返回false,并在运行循环结束时返回true

如果两个堆栈的大小不同,还必须返回false。

公共布尔值等于(堆栈其他){
public boolean equals(Stack<T> other) {
    //If they point to the same object return true
    if (stack == other) return true;
    //Check for nulls
    if (stack == null || other == null) return false;
    //If the stacks are not the same length, then they won't be equal, easy first test case
    if (stack.length != other.size()) return false;

    for(int i=0; i < stack.length; i++) {
           //Step through each item in both stacks, if any don't match return false
           if(!stack[i].equals(other.stack[i]) ) {
                  return false;
           }
    }

    //Haven't returned yet, they must be equal
    return true;
}
//如果它们指向同一对象,则返回true if(stack==other)返回true; //检查空值 if(stack==null | | other==null)返回false; //如果堆栈的长度不同,那么它们就不会相等,这是第一个简单的测试用例 if(stack.length!=other.size())返回false; for(int i=0;i
公共布尔等于(堆栈其他){
//如果它们指向同一对象,则返回true
if(stack==other)返回true;
//检查空值
if(stack==null | | other==null)返回false;
//如果堆栈的长度不同,那么它们就不会相等,这是第一个简单的测试用例
if(stack.length!=other.size())返回false;
for(int i=0;i
您可以在java中使用String.valueOf()方法,该方法将不同类型的值转换为字符串

如果s和t是两个堆栈对象

String a = String.valueOf(s);
String b= String.valueOf(t);

return a.equals(b); // will return true if stacks are equal 
如果堆栈中的任何一个为空,此方法也会注意。

您可以使用String.valueOf()方法在java中,此方法将不同类型的值转换为字符串

如果s和t是两个堆栈对象

String a = String.valueOf(s);
String b= String.valueOf(t);

return a.equals(b); // will return true if stacks are equal 

如果堆栈中的任何元素为空,此方法也会注意。

颠倒逻辑。您将在第一个元素上返回
true
。对于初学者来说,如果堆栈中的第一个元素相等,您可能不希望返回true。@b您编写了一些不需要的内容。此外,您可以快速测试以下三种情况a一开始:这是空的,另一个是空的,这是空的,另一个不是空的,这不是空的,另一个是你需要处理.equals(null)的情况。而且,这可能只是我是个混蛋:这是
peek()
,不是
peak()
。颠倒您的逻辑。您在第一个元素上返回
true
。对于初学者来说,如果堆栈中的第一个元素相等,您可能不想返回true。@b您编写了一些不需要的内容。此外,您可以在开始时快速测试以下三种情况:这是空的,另一个是空的,这是空的空,其他不是,这不是空的,而你需要处理.Error(null)的情况。而且,这只是我是一个挺举:它是<代码> PEEK()/<代码>,而不是<代码> PEAK()/代码>。此外,考虑使这个空参数安全和空元素安全。@ KvundItRigLa<代码>如果(Stase= null和& = = null)return true;
@KevinDiTraglia当!stack[i].equals(Other.stack[i]:用于静态内容或其他内容的非静态变量时出错。只需使用if(stack==Other)return true启动该方法;如果它们指向同一个对象,则它们相等,并且这也处理了两者同时存在的情况