Java 使用BlueJ,我可以';流行音乐'&';推动';但是可以';t';顶部';?

Java 使用BlueJ,我可以';流行音乐'&';推动';但是可以';t';顶部';?,java,Java,(这里是学生软件开发人员!) 我们(我们的类)最近使用BlueJ开始了一个新的java主题。这对我来说是全新的,但到目前为止,我很享受,也有点理解 我们必须使用具有5个选项的菜单创建垂直堆叠阵列: 1-推 2-流行音乐 3-顶部 4-显示 5-退出 除了“Top”(在堆栈/数组的顶部显示整数)之外,我已经成功地对所有内容进行了编码 我的讲师给了我们这样一个提示:“你可以‘Pop’然后‘Push’以达到‘Top’”,但我不太确定如何让它发挥作用 这是我的菜单代码: public static vo

(这里是学生软件开发人员!)

我们(我们的类)最近使用BlueJ开始了一个新的java主题。这对我来说是全新的,但到目前为止,我很享受,也有点理解

我们必须使用具有5个选项的菜单创建垂直堆叠阵列: 1-推 2-流行音乐 3-顶部 4-显示 5-退出

除了“Top”(在堆栈/数组的顶部显示整数)之外,我已经成功地对所有内容进行了编码

我的讲师给了我们这样一个提示:“你可以‘Pop’然后‘Push’以达到‘Top’”,但我不太确定如何让它发挥作用

这是我的菜单代码:

public static void main()
    {
        int option;
        Array a = new Array();
        String []menuitems = {"1 - Push","2 - Pop","3 - Top","4 - Display","5 - Quit"};            
        Menu m = new Menu(menuitems,5);

        a.add(4);a.add(2);a.add(28);a.add(15);

        do
        {
            clrscr();
            option = m.showMenu();
            if ( option == 1 )                                
                doPush(a);   
            if ( option == 2 )                                
                doPop(a);   
            if ( option == 3 )                                
                doTop(a);   
            if ( option == 4 )                
            {                    
                a.display();
                pressKey();
            }
        }
        while ( option != 5 );
        System.out.println("Done - You Can Now Close");
    }
这是我的推送代码:

public static void doPush(Array a)
    {
        if ( a.isFull() )
                {
                    System.out.print("\nArray Full!");                        
                }
                else {
                    int item;
                    System.out.print("Enter number to push: ");
                    item = Genio.getInteger();

                    if ( a.addToFront(item) == false)
                        System.out.print("\nArray Is Full!");
                    System.out.print("\nArray with new value: \n");
                    a.display();
                }
                pressKey();
    }
这是我的Pop代码:

public static void doPop(Array a)
    {
        if ( a.isEmpty() ) {
            System.out.println("\nArray is Empty!");
            pressKey();
            return;
        }
        else
        {
            int item;
            item = Genio.getInteger();
            System.out.println("\nArray popped!");
        }


                pressKey();
    }

我建议在
Array
类本身上定义
pop()
push()
方法。然后您可以定义
top()
,如下所示:

public int top() {
    int topValue = pop();
    push(topValue);
    return topValue;
}
i、 e.将其从堆栈中弹出并记下值,然后将其推回到堆栈中。这不是一个超级高效的实现,但如果这是他的暗示,那么我会这样做

我还建议使用异常,而不是
System.out.println()
作为错误条件,并为使用数组的
堆栈定义特定类:

public class Stack {

    private Array array = new Array();

    public int push(int item) {
        if (!array.addToFront(item)) {
            throw new IllegalStateException("Stack is full");
        }
        /* TODO: It would be better (more idiomatic) for the Array.addToFront()
         * method threw this exception rather than returning a boolean.
         */ 
    }

    public int pop() {
        assertStackNotEmpty();

        // TODO: Remove the item from the front of the array
        //       and shuffle everything along

        return item;
    }

    public int peek() {
        assertStackNotEmpty();
        return array.get(0);
    }

    /**
     * Lecturer's suggested implementation of peek()
     */
    public int top() {
        int item = pop();
        push(item);
        return item;
    }

    private void assertStackNotEmpty() {
       if (array.isEmpty()) {
           throw new EmptyStackException("Stack is empty");
       }
    }
}

只需弹出元素,返回它,然后将其推回到数据结构上。我对此不确定。如果这样做,有什么区别/好处?只是把它们切分到类中?关键是要把逻辑放在最适合容纳它的对象(有数据的对象)中。有一个JDK类,它的接口与我建议的类似。top不就是指向第一个元素吗?为什么要弹出并推送它,只返回数组[0]?一个原因是避免重复对空堆栈可能执行的任何错误处理。