我的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);
}
}
答案 0 :(得分:0)
正如其他人所说:使用调试器,但在这种情况下查看代码很明显有什么不对,理解这一点: