2014-10-03 138 views
0

我的addFront方法我认为是万物打破的原因。 isEmpty()方法是构建的,但我不确定这是否是它破坏的原因。为什么我的环形缓冲区/循环缓冲区在java打破?

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); 
    } 

} 
+0

这听起来像是学习使用调试器的绝佳机会。 – NPE 2014-10-03 05:59:59

+0

描述问题。发布loke“修复我的代码”在这里是offtopic。 – talex 2014-10-03 06:00:08

+0

@NPE我对NetBeans调试器说异常线程 “main” 显示java.lang.NullPointerException \t在ExpandableArrayBuffer.addFront(ExpandableArrayBuffer.java:34) \t在CBufferApp.main(CBufferApp.java:10) 与线34意思是addFront和第10行,意思是isEmpty()方法 – Frightlin 2014-10-03 06:20:18

回答

0

正如其他人说:使用调试器,但在这种情况下看代码很明显有什么不对,要了解这一点:

  1. 将数组视为对象,因此将元素定义为类型为String []的变量使其成为可引用字符串数组的变量
  2. 当引用变量被定义为类的属性时,与空指针初始化
  3. 到元素elements.length所有引用参考数组的长度,因为无处在你的代码的元素参考
  4. 你真正喜欢创建新的String字符串数组[5]和分配这对元素来说,所有对elements.length的引用都将导致NullPointerException异常