Java 通过比较堆栈和队列确定回文

Java 通过比较堆栈和队列确定回文,java,stack,queue,palindrome,Java,Stack,Queue,Palindrome,因此,对于我的赋值,我必须编写一个程序,使用StackArrayBase.java和QueueArrayBase.java的实例,向它们发送一个字符串,并比较dequeue()和pop()方法返回的结果,以确定该字符串是否为回文。我已经编写了程序,但它没有返回正确的布尔值,请帮助 public class IsPalindrome{ public static void main(String[]args){ String str = new String("abcba"

因此,对于我的赋值,我必须编写一个程序,使用StackArrayBase.java和QueueArrayBase.java的实例,向它们发送一个字符串,并比较dequeue()和pop()方法返回的结果,以确定该字符串是否为回文。我已经编写了程序,但它没有返回正确的布尔值,请帮助

public class IsPalindrome{
    public static void main(String[]args){
        String str = new String("abcba");
        String str2 = new String("abcde");
        System.out.println(isPal(str));
        System.out.println(isPal(str2));
    }
    public static boolean isPal(String str)
    {
        StackArrayBased stack = new StackArrayBased();
        QueueArrayBased queue = new QueueArrayBased();

        String s = new String();
        for (int i = 0; i < str.length( ); i++) {
            s = "" + str.charAt(i);
            System.out.println(s);
            queue.enqueue(s);
            stack.push(s);
        }
        // start to compare
        while (!queue.isEmpty( )) {
            if (queue.dequeue( ) != stack.pop( ))
                return false;
        }
        // finished w/ empty queue (and empty stack)
        return true;
    }
}
公共类IsPalindrome{
公共静态void main(字符串[]args){
String str=新字符串(“abcba”);
字符串str2=新字符串(“abcde”);
系统输出println(isPal(str));
系统输出println(isPal(str2));
}
公共静态布尔值isPal(字符串str)
{
StackArrayBased stack=新的StackArrayBased();
QueueArrayBased queue=新的QueueArrayBased();
字符串s=新字符串();
对于(inti=0;i
您正在向队列和堆栈添加字符串,通常应避免使用标准的字符串相等性检查(因为它们比较对象标识而不是内容)

更改:

if (queue.dequeue( ) != stack.pop( ))
致:

例如,此代码(稍作修改)工作正常:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Test   {
    public static void main(String[]args){
        String str = new String("abcba");
        String str2 = new String("abcde");
        System.out.println(isPal(str));
        System.out.println(isPal(str2));
    }
    public static boolean isPal(String str)
    {
        Stack<String> stack = new Stack<String>();
        Queue<String> queue = new LinkedList<String>();

        String s = new String();
        for (int i = 0; i < str.length( ); i++) {
            s = "" + str.charAt(i);
            System.out.println(s);
            queue.add(s);
            stack.push(s);
        }
        // start to compare
        while (!queue.isEmpty( )) {
            if (!queue.remove().equals(stack.pop( )))
                return false;
        }
        // finished w/ empty queue (and empty stack)
        return true;
    }
}

第一次使用!(queue.dequeue().equals(stack.pop())而不是!=。另外,new String()是多余的。@romantsegelskyi是的,但我的教授坚持用这种方式初始化字符串。在他们的控制下,按照教授的话去做。然后,当你接触到现实世界时,扔掉他们可能会让你染上:-)快速问题的任何积垢。需要新的绳子吗?因为s=“”+str.charAt(i);无论如何都将返回一个字符串并覆盖该字符串。@romantsegelskyi,可能不会,但它与当前的问题无关。换句话说,这可能是浪费,但这不是这里需要解决的问题。耶!非常感谢。嗯,这太明显了。我是哑巴。谢谢你没有像其他人一样骚扰我,因为我犯了一个愚蠢的错误。@Joey,我们都曾经是初学者,有时人们会忘记这一点。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Test   {
    public static void main(String[]args){
        String str = new String("abcba");
        String str2 = new String("abcde");
        System.out.println(isPal(str));
        System.out.println(isPal(str2));
    }
    public static boolean isPal(String str)
    {
        Stack<String> stack = new Stack<String>();
        Queue<String> queue = new LinkedList<String>();

        String s = new String();
        for (int i = 0; i < str.length( ); i++) {
            s = "" + str.charAt(i);
            System.out.println(s);
            queue.add(s);
            stack.push(s);
        }
        // start to compare
        while (!queue.isEmpty( )) {
            if (!queue.remove().equals(stack.pop( )))
                return false;
        }
        // finished w/ empty queue (and empty stack)
        return true;
    }
}
a
b
c
b
a
true
a
b
c
d
e
false