如何将字符串中的字符放入基于数组的 ADT 堆栈版本

发布于 2024-10-25 04:45:39 字数 1913 浏览 1 评论 0原文

因此,对于我的作业,我需要使用基于数组的 ADT 堆栈版本来创建一个名为 myStack 的堆栈。然后,我需要创建一个名为 str 的字符串变量,并将其分配给字符串“abcdefg”,并访问字符串中的字符,并将每个字符推入堆栈,从第一个字符开始。当所有字符都进入堆栈后,我需要将它们弹出并显示在一行上。这是我正在使用的:

public class StackArrayBased
{
  private static final int MAX_STACK = 7 ;
  private Object items [ ] ;
  private int top ;

  public StackArrayBased ( )
  {
    items = new Object [ MAX_STACK ] ;
    top = -1 ;
  }

  public boolean isEmpty ( )
  {
    return top < 0 ;
  }

  public boolean isFull ( )
  {
    return top == MAX_STACK - 1 ;
  }

  public void push ( Object newItem ) throws StackException
  {
    if ( ! isFull ( ) )
      items [ ++ top ] = newItem ;
    else
      throw new StackException ( "StackException on push: stack is full" ) ;
  }

  public void popAll ( )
  {
    items = new Object [ MAX_STACK ] ;
    top = -1 ;
  }

  public Object pop ( ) throws StackException
  {
    if ( ! isEmpty ( ) )
      return items [ top -- ] ;
    else
      throw new StackException ( "StackException on pop: stack is empty" ) ;
  }

  public Object peek ( ) throws StackException
  {
    if ( ! isEmpty ( ) )
      return items [ top ] ;
    else
      throw new StackException ( "StackException on peek: stack is empty" ) ;
  }
}

以及:

public class StackException extends RuntimeException
{
  public StackException ( String s )
  {
    super ( s ) ;
  }
}

这是我到目前为止所拥有的,但我无法理解为什么我会遇到异常并且它没有正确运行:

public class StackArrayBasedTester
{
  public static void main ( String [ ] args )
  {
    String str = "abcdefg" ;
    StackArrayBased myStack = new StackArrayBased ( ) ;
    Integer i = 1 ;
    Character c ;
    for ( i = 1 ; i <= 7 ; i++ )
      if ( ! myStack.isFull ( ) )
        myStack.push ( c = str.charAt ( i ) ) ;
    while ( ! myStack.isEmpty ( ) )
      System.out.print( " "+myStack.pop ( ) ) ;
  }
}

还有其他方法可以做到这一点吗?

so for my assignment, I need to used the array-based version of the ADT stack to create a stack called myStack. I then need to create a string variable called str and assign it to the string "abcdefg" and access the characters in the string and push each character onto the stack, starting with the first. After all the characters are in the stack, I need to pop and display each of them on one line. Here is what I'm using:

public class StackArrayBased
{
  private static final int MAX_STACK = 7 ;
  private Object items [ ] ;
  private int top ;

  public StackArrayBased ( )
  {
    items = new Object [ MAX_STACK ] ;
    top = -1 ;
  }

  public boolean isEmpty ( )
  {
    return top < 0 ;
  }

  public boolean isFull ( )
  {
    return top == MAX_STACK - 1 ;
  }

  public void push ( Object newItem ) throws StackException
  {
    if ( ! isFull ( ) )
      items [ ++ top ] = newItem ;
    else
      throw new StackException ( "StackException on push: stack is full" ) ;
  }

  public void popAll ( )
  {
    items = new Object [ MAX_STACK ] ;
    top = -1 ;
  }

  public Object pop ( ) throws StackException
  {
    if ( ! isEmpty ( ) )
      return items [ top -- ] ;
    else
      throw new StackException ( "StackException on pop: stack is empty" ) ;
  }

  public Object peek ( ) throws StackException
  {
    if ( ! isEmpty ( ) )
      return items [ top ] ;
    else
      throw new StackException ( "StackException on peek: stack is empty" ) ;
  }
}

as well as:

public class StackException extends RuntimeException
{
  public StackException ( String s )
  {
    super ( s ) ;
  }
}

This is what I have so far, but I cannot understand why I'm getting exceptions and it isn't running correctly:

public class StackArrayBasedTester
{
  public static void main ( String [ ] args )
  {
    String str = "abcdefg" ;
    StackArrayBased myStack = new StackArrayBased ( ) ;
    Integer i = 1 ;
    Character c ;
    for ( i = 1 ; i <= 7 ; i++ )
      if ( ! myStack.isFull ( ) )
        myStack.push ( c = str.charAt ( i ) ) ;
    while ( ! myStack.isEmpty ( ) )
      System.out.print( " "+myStack.pop ( ) ) ;
  }
}

Is there any other way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2024-11-01 04:45:39

您遇到什么样的异常?

charAt 从 0 开始计数。当您执行 str.charAt(7) 时,您将收到 IndexOutOfBoundsException。

此外,最好在 i i i i i i i i i i i i i < 上循环。 str.length(),以防您想修改 str 保留不同的值。

What kind of exception are you getting?

charAt starts counting from 0. When you do str.charAt(7), you'll get an IndexOutOfBoundsException.

Additionally, it might be better to loop on i < str.length(), in case you want to modify str hold a different value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文