JAVA - 如何将字符串压入堆栈?
JAVA - How do you push a string into a stack?
我有一项任务是将字符串压入堆栈。我创建了一个将存储数字的程序,但我无法找出定义数组以获取字符串的正确方法。这是我的代码。我的 Java 生锈了,所以我试图记住 2 年前我第一次 java class 时的这一切。我确信它非常简单,但我无法在网上找到任何将字符串存储在堆栈中的东西,供我查看如何操作。感谢您的帮助!
public class stackx {
private int maxSize; //number of items in stack
private int[] stackArray;
private int top; // top of stack
public stackx(int arraySize) {
maxSize = arraySize;
stackArray = new int[maxSize];
top = -1;
}
public void push(int a) { //put value on top of stack
if (top == maxSize - 1)
{
System.out.println("Stack is full");
} else {
top = top + 1;
stackArray[top] = a;
}
}
public int pop() { //take item from top of stack
if (!isEmpty())
return stackArray[top--]; // access item, decrement top
else {
System.out.println("Stack is Empty");
}
}
public int peek() //peek at the top of the stack
{
return stackArray[top];
}
public boolean isEmpty() { //true if stack is empty
return top == -1;
}
public void display() {
for (int i = 0; i <= top; i++) {
System.out.print(stackArray[i] + " ");
}
System.out.println();
}
} // End class stackx
**Driver class Here**
public class practicestack {
public static void main(String[] args) {
stackx newStack = new stackx(5);
newStack.push(redShirt);
newStack.push(greenShirt);
newStack.push(yellowPants);
newStack.push(purpleSocks);
newStack.push(pinkSocks);
stackx.peek();
//Display the Full Stack
newStack.display();
//Test removing a value using pop method
newStack.pop();
newStack.display();
}
}
您的堆栈只需要 int
秒。如果你想存储任何东西,你需要它花费 Object
s。 Java 不允许你操作指针,所以你不能像 C/C++ 那样只使用 int
。您还可以使用泛型,例如public class stackx<T>
,这会给你 stackx<String> newStack = new stackx<>(5);
.
这应该很容易,我会给你一个小提示,如果你还是想不通,我会post整个代码
当你声明这样的东西时
private int[] stackArray;
并使用此数组推送和弹出您的项目,因为这是整数数组,您只能将此实现用于整数。
现在你的要求是压入和弹出字符串,所以基本上你应该做这样的事情。
private String[] stackArray;
注意:您的 Push 和 pop 方法也会发生同样的变化,这些变化很小
希望对您有所帮助!
祝你好运。
只需将int
修改为String
即可。这里是Demo。
我有一项任务是将字符串压入堆栈。我创建了一个将存储数字的程序,但我无法找出定义数组以获取字符串的正确方法。这是我的代码。我的 Java 生锈了,所以我试图记住 2 年前我第一次 java class 时的这一切。我确信它非常简单,但我无法在网上找到任何将字符串存储在堆栈中的东西,供我查看如何操作。感谢您的帮助!
public class stackx {
private int maxSize; //number of items in stack
private int[] stackArray;
private int top; // top of stack
public stackx(int arraySize) {
maxSize = arraySize;
stackArray = new int[maxSize];
top = -1;
}
public void push(int a) { //put value on top of stack
if (top == maxSize - 1)
{
System.out.println("Stack is full");
} else {
top = top + 1;
stackArray[top] = a;
}
}
public int pop() { //take item from top of stack
if (!isEmpty())
return stackArray[top--]; // access item, decrement top
else {
System.out.println("Stack is Empty");
}
}
public int peek() //peek at the top of the stack
{
return stackArray[top];
}
public boolean isEmpty() { //true if stack is empty
return top == -1;
}
public void display() {
for (int i = 0; i <= top; i++) {
System.out.print(stackArray[i] + " ");
}
System.out.println();
}
} // End class stackx
**Driver class Here**
public class practicestack {
public static void main(String[] args) {
stackx newStack = new stackx(5);
newStack.push(redShirt);
newStack.push(greenShirt);
newStack.push(yellowPants);
newStack.push(purpleSocks);
newStack.push(pinkSocks);
stackx.peek();
//Display the Full Stack
newStack.display();
//Test removing a value using pop method
newStack.pop();
newStack.display();
}
}
您的堆栈只需要 int
秒。如果你想存储任何东西,你需要它花费 Object
s。 Java 不允许你操作指针,所以你不能像 C/C++ 那样只使用 int
。您还可以使用泛型,例如public class stackx<T>
,这会给你 stackx<String> newStack = new stackx<>(5);
.
这应该很容易,我会给你一个小提示,如果你还是想不通,我会post整个代码 当你声明这样的东西时
private int[] stackArray;
并使用此数组推送和弹出您的项目,因为这是整数数组,您只能将此实现用于整数。
现在你的要求是压入和弹出字符串,所以基本上你应该做这样的事情。
private String[] stackArray;
注意:您的 Push 和 pop 方法也会发生同样的变化,这些变化很小
希望对您有所帮助!
祝你好运。
只需将int
修改为String
即可。这里是Demo。