堆栈使用pop()方法和推阵列() [英] Stack array using pop() and push()

查看:94
本文介绍了堆栈使用pop()方法和推阵列()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我已经为程序创建的使用堆栈2班的一个问题。我得到的第一个问题是,当我尝试运行该程序,我得到一个运行时错误。它的种类一件很难的事情要问,因为它做的几件事情。它要求用户输入号码添加到堆栈和检查,如果堆栈满或空。我可能还需要帮助阵列复制。

I have a problem with 2 classes that i've created for a program the uses the stack. The first problem that I get is that when I try to run the program I get a runtime error. Its kind of a difficult thing to ask because it doing several things. It asks for user input to add numbers to the stack and checking if the stack is full or empty. I also may need help to copy the array.

异常线程mainjava.lang.ArrayIndexOutOfBoundsException:-1
    在IntegerStack.push(IntegerStack.java:24)
    在Lab15.main(Lab15.java:38)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at IntegerStack.push(IntegerStack.java:24) at Lab15.main(Lab15.java:38)

这是运行该程序的主类。

This is the main class that runs the program.

import java.util.Scanner;


public class Lab15 {

    public static void main(String[] args)
    {
        System.out.println("***** Playing with an Integer Stack *****");
        final int SIZE = 5;
        IntegerStack myStack = new IntegerStack(SIZE);
        Scanner scan = new Scanner(System.in);

        //Pushing integers onto the stack
        System.out.println("Please enter an integer to push onto the stack - OR - 'q' to Quit");
        while(scan.hasNextInt())
        {
            int i = scan.nextInt();
            myStack.push(i);
            System.out.println("Pushed "+ i);
        }

        //Pop a couple of entries from the stack
        System.out.println("Lets pop 2 elements from the stack");
        int count = 0;
        while(!myStack.isEmpty() && count<2)
        {
            System.out.println("Popped "+myStack.pop());
            count++;
        }

        scan.next(); //Clearing the Scanner to get it ready for  further input.

        //Push a few more integers onto the stack
        System.out.println("Push in a few more elements - OR - enter q to quit");
        while(scan.hasNextInt())
        {
            int i = scan.nextInt();
            myStack.push(i);
            System.out.println("Pushed "+ i);
        }

        System.out.println("\nThe final contentes of the stack are:");
        while(!myStack.isEmpty())
        {
            System.out.println("Popped "+myStack.pop());
        }

    }

}

这是添加数字到堆栈这正是具有probrelms的类。这是我需要帮助复制阵列。最后。

This is the class that is adding the numbers to the stack which is what has the probrelms. This is where i may need help copying the array. At the end.

import java.util.Arrays;

public class IntegerStack 
{
    private int stack [];
    private int top; 

    public IntegerStack(int SIZE) 
    {
        stack = new int [SIZE];
        top = -1;
    }

    public void push(int i) 
    {
        if (top == stack.length)
        {
            extendStack();
        }

        stack[top]= i;
        top++;
    }

    public int pop() 
    {
        top --;
        return stack[top];
    }

    public int peek()
    {
        return stack[top];
    }

    public boolean isEmpty() 
    {
        if ( top == -1);
        {
            return true;
        }
    }

    private void extendStack()
    {
        int [] copy = Arrays.copyOf(stack, stack.length);
    }
}

任何帮助或方向将是AP preciated。

Any help or direction will be appreciated.

推荐答案

为您的Stack实现更好的解决方案。

Better solution for your Stack implementation

import java.util.List;
import java.util.ArrayList;
public class IntegerStack 

{

    private List<Integer> stack;

    public IntegerStack(int SIZE) 
    {
        stack = new ArrayList<Integer>(SIZE);
    }

    public void push(int i) 
    {

       stack.add(0,i);
     }

     public int pop() 
     { 
        if(!stack.isEmpty()){
           int i= stack.get(0);
           stack.remove(0);
           return i;
        } else{
           return -1;// Or any invalid value
        }
     }

     public int peek()
     {
        if(!stack.isEmpty()){
           return stack.get(0);
        } else{
           return -1;// Or any invalid value
        }
     }


     public boolean isEmpty() 
     {
       stack.isEmpty();
     }

 }

如果你必须使用阵列......这里有你的code的问题和可能的解决方案

If you have to use Array... Here are problems in your code and possible solutions

import java.util.Arrays;
public class IntegerStack 
{

    private int stack [];
    private int top; 

    public IntegerStack(int SIZE) 
   {
    stack = new int [SIZE];
    top = -1; // TOp SHOULD be 0.. if you keep it -1 problem will arise when SIZE passed as 0 
    // and your calls push method -1===0 will false and code will ry to add element to Stack .. 
     /**Solution top=0; */
    }

public void push(int i) 
{
    if (top == stack.length)
    {
        extendStack();
    }

       stack[top]= i;
        top++;

}

public int pop() 
{
    top --; // here you are reducing the top bfore giving the Obeject back 
   /*Solution 
      if(!isEmpty()){
      int value= stack[top];
       top --;
     return value; 
    } else{
      return -1;// OR invalid value
    }
   */
    return stack[top];
}

public int peek()
{
    return stack[top]; // Problem when stack Empty or size 0
    /*Solution 
       if(!isEmpty()){
         return stack[top];
       }else{
         return -1;// Or any invalid value
       }
    */


}


public boolean isEmpty() 
{
    if ( top == -1); // problem... we changeddefualt to 0 so here it need to 0 and no semicolon after if
   /* Solution if(top==0) */
    {
        return true;
    }
}

private void extendStack()
{

    int [] copy = Arrays.copyOf(stack, stack.length); // No Length Changes... so no change in array.
  /*Solution  
    stack=Arrays.copyOf(stack, stack.length+1); 
   */
     }

     }

这篇关于堆栈使用pop()方法和推阵列()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆