使用堆栈反转队列时出现空指针异常

时间:2016-07-31 05:46:16

标签: data-structures stack queue reverse

我正在练习如何使用数组实现队列。我已经轻松实现了如何将队列中的元素入队和出列。但是我在使用堆栈实现队列反向时遇到了异常

lib/ext

错误日志是 -

java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
    at org.apache.http.util.Asserts.check(Asserts.java:34)
    at org.apache.http.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:166)
    at org.apache.http.impl.conn.BasicClientConnectionManager$1.getConnection(BasicClientConnectionManager.java:148)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:424)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at org.jmeterplugins.repository.PluginManager.getJSON(PluginManager.java:137)
    at org.jmeterplugins.repository.PluginManager.load(PluginManager.java:78)
    at org.jmeterplugins.repository.PluginManagerDialog.componentShown(PluginManagerDialog.java:178)
    at java.awt.Component.processComponentEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Window.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.WaitDispatchSupport$2.run(Unknown Source)
    at java.awt.WaitDispatchSupport$4.run(Unknown Source)
    at java.awt.WaitDispatchSupport$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.WaitDispatchSupport.enter(Unknown Source)
    at java.awt.Dialog.show(Unknown Source)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at java.awt.Dialog.setVisible(Unknown Source)
    at org.jmeterplugins.repository.PluginManagerMenuItem.actionPerformed(PluginManagerMenuItem.java:41)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

您的出列方法存在问题: 1.你在前面= 0并试图访问前-1,即-1 结果引发了异常。

  1. 删除了stack.get,因为它不是必需的。
  2. 更正了工作代码。

    public class QueueImpl {
            private int capacity;
            int queueArr[];
            int front = 0;
            int rear = -1;
            int currentSize = 0;
    
            QueueImpl(int queueSize) {
                this.capacity = queueSize;
                queueArr = new int[this.capacity];
            }
    
            public void enqueue(int data) {
                if (isQueueFull()) {
                    System.out.println("Overflow");
                    return;
                } else {
                    rear = rear + 1;
                    if (rear == capacity - 1) {
                        rear = 0;
                    }
                    queueArr[rear] = data;
                    currentSize++;
                    System.out.println("Element " + data + " is pushed to Queue !");
                }
    
            }
    
            public int dequeue() {
                int element=-1;
                if (isQueueEmpty()) {
                    System.out.println("UnderFlow");
                } else {
                    element = queueArr[front];
                    front=front+1;
                    if (front == capacity - 1) {
                        System.out.println("Pop operation done ! removed: "
                                + queueArr[front - 1]);
                        front = 0;
                    } else {
                        System.out.println("Pop operation done ! removed: "
                                + queueArr[front - 1]);
                    }
                    currentSize--;
                }
                return element;
    
            }
    
            private boolean isQueueEmpty() {
                boolean status = false;
                if (currentSize == 0) {
                    status = true;
                }
                return status;
            }
    
            private boolean isQueueFull() {
                boolean status = false;
                if (currentSize == capacity) {
                    status = true;
                }
                return status;
            }
    
            public static void main(String arg[]) {
                QueueImpl queueImpl = new QueueImpl(5);
                queueImpl.enqueue(5);
                queueImpl.enqueue(2);
                queueImpl.enqueue(9);
                queueImpl.enqueue(1);
    
                queueImpl.printQueue(queueImpl);
                queueImpl.reverse(queueImpl);
                queueImpl.printQueue(queueImpl);
            }
    
            private void printQueue(QueueImpl queueImpl) {
                System.out.println(queueImpl.toString());
            }
    
            @Override
            public String toString() {
                return "Queue [front=" + front + ", rear=" + rear + ", size="
                        + currentSize + ", queue=" + Arrays.toString(queueArr) + "]";
            }
    
            private QueueImpl reverse(QueueImpl queueImpl)
                    throws ArrayIndexOutOfBoundsException {
                Stack<Integer> stack = new Stack<Integer>();
                while (!queueImpl.isQueueEmpty()) {
                    stack.push(queueImpl.dequeue());
                }
                while (!stack.isEmpty()) {
                    queueImpl.enqueue(stack.pop());
                }
                return queueImpl;
            }
    
        }