如何将字符串中的字符放入基于数组的 ADT 堆栈版本
因此,对于我的作业,我需要使用基于数组的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到什么样的异常?
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 dostr.charAt(7)
, you'll get an IndexOutOfBoundsException.Additionally, it might be better to loop on
i < str.length()
, in case you want to modifystr
hold a different value.