简体   繁体   English

打印堆栈没有弹出元素java

[英]printing stack without popping elements java

for a task I have to write a method which prints the stack, that part is easy 对于一个任务,我必须编写一个打印堆栈的方法,这部分很容易

public void print(stack s)
{
   while(!isEmpty())
   {
      System.out.println(s.peek());
      s.pop();
   }

}

The problem is that after I printed the stack, my task is to print the bottom element on the stack, which isn't there any more cause I used s.pop() in my print method. 问题是,在我打印堆栈之后,我的任务是在堆栈上打印底部元素,这不再是因为我在我的print方法中使用了s.pop()。 This is my code for printing the bottom element. 这是我打印底部元素的代码。

public void bottom(stack s)
{
  if(isEmpty())
   {
     System.out.println("Stack is empty");
   }
  else
   {
     System.out.println(stackArray[0]);
   }
}

My question is: How should i modifie the print method so I don't have to pop the elements from the stack? 我的问题是:我应该如何修改print方法,这样我就不必从堆栈中弹出元素了? Or is there another way to make it so that the stack still holds my elements after using print method? 或者是否有另一种方法可以使堆栈在使用print方法后仍保留我的元素?

as resquested this is the stack we're using in our classes(most of it is in dutch): 如果被请求这是我们在我们的类中使用的堆栈(大多数是在荷兰语中):

public class MyStack
{
    protected Object[ ] stackArray;
    protected int top;
    private int grootte;
    private static final int DEFAULT_GROOTTE = 10;

    public MyStack( )
    {
        grootte = DEFAULT_GROOTTE;
        stackArray = new Object[grootte];
        top = 0;
    }

    public boolean isEmpty( )
    {
        if (top == 0)
                return true;
            else 
                return false;
    }

    public void push(Object e)
    {
        if (top == grootte)
            allocateMore( );
        stackArray[top] = e;
        top++;
    }

    public Object pop( )
    {
            if(isEmpty( ))
            {
                System.out.println("Stack leeg : er kan geen element van de stack afgehaald worden.");
                return null;
            }
                    top--;
                    return stackArray[top];

}
    public Object peek( )
    {
            if(isEmpty( ))
            {
                System.out.println("Stack leeg : er kan geen topelement van de stack getoond worden.");
                return null;
            }
                    return stackArray[top-1];
    }

    public int size( )
    {
        return top;
    }

    private void allocateMore( )
    {
        Object[ ] original = stackArray;
        grootte = grootte * 2;
        stackArray = new Object[ grootte];
        for(int i = 0; i < grootte/2; i++)
        {
            stackArray[i] = original[i];
    }
    }

}

since my rep isn't high enough to answer my own question a quick edit 因为我的代表不够高,不能回答我自己的问题快速编辑

I think I've found an other way to print the stack using this 我想我已经找到了另一种使用它来打印堆栈的方法

public void print(stack s)
{
 for(int i =top-1; i>=0;i--)
   System.out.println(stackArray[i]);
}

it probably isn't the best way to do it, but it's working :P 它可能不是最好的方法,但它正在起作用:P

If you use the built-in java.util.Stack type, then this derives from Vector , so you can use getElement(int) to read elements at any stack depth. 如果使用内置的java.util.Stack类型,那么它派生自Vector ,因此您可以使用getElement(int)读取任何堆栈深度的元素。

If this is your own code, you will have to add a method to do the same. 如果这是您自己的代码,则必须添加一个方法来执行相同的操作。

Alternatively, you can pop the elements into another stack or a List type and then rebuild the stack after printing but that would be very inefficient and your teacher will most probably frown about such a solution. 或者,您可以将元素弹出到另一个堆栈或List类型中,然后在打印后重建堆栈,但这样效率非常低,您的老师很可能对这样的解决方案感到沮丧。

如果你只想看到内容而没有任何花哨的东西,有一个简单的解决方法。

System.out.println(Arrays.toString(myStack.toArray()));
if (!_stack.empty())

Check whether the Stack is Empty 检查堆栈是否为空

for(int i=_stack.size()-1; i>=0;i--)  
System.out.println(_stack.get(i));

getting stack values 获得堆栈值

使用Iterator遍历堆栈,因为它们可以处理任何Collection对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM