【问题标题】:Why is my ring buffer / circular buffer in java breaking?为什么我在 java 中的环形缓冲区/循环缓冲区会中断?
【发布时间】:2014-10-03 05:49:39
【问题描述】:

我相信我的 addFront 方法是一切都崩溃的原因。 isEmpty() 方法已构建,但我不确定这是否是它破坏的原因。

public class ExpandableArrayBuffer implements ExpandableBuffer {
     private static final int DEFAULT_CAPACITY = 10;
    String[] elements; 

    int size = 0;
    int rear = 0;
    int front = 0; 

    //constructor
      public ExpandableArrayBuffer(int size) {
            this.size = size;   
        // Make it that size

    }
      public ExpandableArrayBuffer(){
       this(DEFAULT_CAPACITY);
      }

    @Override
    public boolean isEmpty() {

        return front == rear; 

        }

    @Override
    public void addFront(String s) {

        if(front<0||front>elements.length){
           int temp=0;
           front=temp;

       }
        if(isEmpty()){
           elements[0]=s;
        }else{

       s=elements[front];
       }
       front--;
       size++;
       }



    @Override
    public void addRear(String s) {
     int avail = (front + size) % elements.length;
        elements[avail] = s;
        size++;
    }

    @Override
    public String removeFront() {
     String answer = elements[front];
     elements[front] = null; 
     front = (front + 1) % elements.length;
     size--;
     return answer;
    }

    @Override
    public String removeRear() {
        String keysersoze = elements [rear];
        elements[rear] = null;
        rear = (rear + size) % elements.length;
        size--;
        return keysersoze;
    }

    public int size(){
        return size;
    }
}

public interface ExpandableBuffer {

    /**
     * Returns true if this buffer contains no elements.
     * @return true if this buffer contains no elements
     */
    boolean isEmpty();

    /**
     * Adds the specified string to the front of this buffer.
     * @param s string to be added to this buffer
     */
    void addFront(String s);

    /**
     * Adds the specified string to the rear of this buffer.
     * @param s string to be added to this buffer
     */
    void addRear(String s);

    /**
     * Removes and returns the string at the front of this buffer.
     * @return the string at the front of this buffer
     */
    String removeFront();

    /**
     * Removes and returns the string at the rear of this buffer.
     * @return the string at the rear of this buffer
     */
    String removeRear();

    /**
     * Returns a string representation of this buffer. The string representation
     * consists of a list of this buffer's Strings in order from front to rear,
     * enclosed in square brackets ("[]"). Adjacent Strings are
     * separated by the characters ", " (comma and space). The letter
     * "R" should appear to the left to indicate the rear of the buffer and the
     * letter "F" should appear to the right to indicate the front of the
     * buffer. Fore example, a buffer containing the strings "A", "B", and "C"
     * would be represented as "R[A, B, C]F".
     *
     * @return a string representation of this buffer
     */    
    String toString();
}

这是一个运行和测试其他类的主类

  public class CBufferApp {
    static String message;
    static ExpandableBuffer buffer;

    public static void main(String[] args) {
        buffer = new ExpandableArrayBuffer();
        message = " 1) Initial buffer";
        print();

        buffer.addFront("A");
        message = " 2) Add A to front";
        print();

        buffer.addFront("B");
        message = " 3) Add B to front";
        print();

        buffer.addFront("C");
        message = " 4) Add C to front";
        print();

        buffer.removeRear();
        message = " 5) Remove rear";
        print();

        buffer.removeRear();
        message = " 6) Remove rear";
        print();

        buffer.removeRear();
        message = " 7) Remove rear";
        print();

        buffer.addFront("D");
        message = " 8) Add D to front";
        print();

        buffer.addFront("E");
        message = " 9) Add E to front";
        print();

        buffer.removeRear();
        message = "10) Remove rear";
        print();

        buffer.removeRear();
        message = "11) Remove rear";
        print();

        buffer.addRear("F");
        message = "12) Add F to rear";
        print();

        buffer.addRear("G");
        message = "13) Add G to rear";
        print();

        buffer.addFront("H");
        message = "14) Add H to front";
        print();

        buffer.addFront("I");
        message = "15) Add I to front";
        print();

        buffer.addFront("J");
        message = "16) Add J to front";
        print();

        buffer.addFront("K");
        message = "17) Add K to front";
        print();

        buffer.addRear("L");
        message = "18) Add L to rear";
        print();

        buffer.addRear("M");
        message = "19) Add M to rear";
        print();

        buffer.addRear("N");
        message = "20) Add N to rear";
        print();

        buffer.addRear("O");
        message = "21) Add O to rear";
        print();

        buffer.addRear("P");
        message = "22) Add P to rear";
        print();
    }

    private static void print(){
        String emptyMessage = "";
        if(buffer.isEmpty()){
            emptyMessage = "Empty";
        }
        System.out.printf("%-23s %-7s", message, emptyMessage);
        System.out.println(buffer);
    }

}

【问题讨论】:

  • 这听起来像是学习使用调试器的绝佳机会。
  • 描述问题。在这里发帖“修复我的代码”是题外话。
  • @NPE 我在 netbeans 上的调试器在 CBufferApp.main(CBufferApp.java:10) 处的 ExpandableArrayBuffer.addFront(ExpandableArrayBuffer.java:34) 处的线程“main”java.lang.NullPointerException 中显示异常34 表示 addFront,第 10 行表示 isEmpty() 方法
  • 单步调试代码、使用断点、查看变量值等,找出问题所在。

标签: java arrays string buffer circular-buffer


【解决方案1】:

正如其他人所说:使用调试器,但在这种情况下,查看代码很明显出了什么问题,要理解这一点:

  1. 数组被视为对象,因此将元素定义为 String[] 类型的变量使其成为可以引用字符串数组的变量
  2. 当引用变量被定义为类的属性时,就像您的元素一样,它们会使用空指针进行初始化
  3. 所有对元素的引用。元素的长度是指元素引用的数组的长度
  4. 因为在您的代码中没有任何地方您实际上创建了一个字符串数组,如 new String[5] 并将其分配给元素,所有对 elements.length 的引用都将导致 NullPointerException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2019-06-08
    • 2015-11-13
    • 2022-11-14
    • 2012-04-04
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多