2016-11-03 104 views
0

我有Java代碼,能夠拿出一個toString如代碼如下所示:修改的toString圓陣緩衝

 public class CircularArrayQueue<T> implements QueueADT<T> 
{ 
    private final int DEFAULT_CAPACITY = 100; 
    private int front, rear, count; 
    private T[] queue; 

    //----------------------------------------------------------------- 
    // Creates an empty queue using the default capacity. 
    //----------------------------------------------------------------- 
    public CircularArrayQueue() 
    { 
     front = rear = count = 0; 
     queue = (T[]) (new Object[DEFAULT_CAPACITY]); 
    } 

    //----------------------------------------------------------------- 
    // Creates an empty queue using the specified capacity. 
    //----------------------------------------------------------------- 
    public CircularArrayQueue (int initialCapacity) 
    { 
     front = rear = count = 0; 
     queue = ((T[])(new Object[initialCapacity])); 
    } 

    //----------------------------------------------------------------- 
    // Adds the specified element to the rear of the queue, expanding 
    // the capacity of the queue array if necessary. 
    //----------------------------------------------------------------- 
    public void enqueue (T element) 
    { 
     if (size() == queue.length) 
     expandCapacity(); 

     queue[rear] = element; 

     rear = (rear+1) % queue.length; 

     count++; 
    } 

    //----------------------------------------------------------------- 
    // Removes the element at the front of the queue and returns a 
    // reference to it. Throws an EmptyCollectionException if the 
    // queue is empty. 
    //----------------------------------------------------------------- 
    public T dequeue() throws EmptyCollectionException 
    { 
     if (isEmpty()) 
     throw new EmptyCollectionException ("queue"); 

     T result = queue[front]; 
     queue[front] = null; 

     front = (front+1) % queue.length; 

     count--; 

     return result; 
    } 

    //----------------------------------------------------------------- 
    // Returns a reference to the element at the front of the queue. 
    // The element is not removed from the queue. Throws an 
    // EmptyCollectionException if the queue is empty. 
    //----------------------------------------------------------------- 
    public T first() throws EmptyCollectionException 
    { 
     if (isEmpty()) 
     throw new EmptyCollectionException ("queue"); 

     return queue[front]; 
    } 

    //----------------------------------------------------------------- 
    // Returns true if this queue is empty and false otherwise. 
    //----------------------------------------------------------------- 
    public boolean isEmpty() 
    { 
     return (count == 0); 
    } 

    //----------------------------------------------------------------- 
    // Returns the number of elements currently in this queue. 
    //----------------------------------------------------------------- 
    public int size() 
    { 
     return count; 
    } 


    //----------------------------------------------------------------- 
    // Returns a string representation of this queue. 
    //----------------------------------------------------------------- 
    public String toString() 
    { 
    String result = ""; 
    int scan = 0; 

    while(scan < count) 
    { 
    if(queue[scan]!=null) 
    { 
     result += queue[scan].toString()+"\n"; 
    } 
    scan++; 
    } 

    return result; 

    } 

    //----------------------------------------------------------------- 
    // Creates a new array to store the contents of the queue with 
    // twice the capacity of the old one. 
    //----------------------------------------------------------------- 
    public void expandCapacity() 
    { 
    T[] larger = (T[])(new Object[queue.length *2]); 

    for(int scan=0; scan < count; scan++) 
    { 
     larger[scan] = queue[front]; 
     front=(front+1) % queue.length; 
    } 

    front = 0; 
    rear = count; 
    queue = larger; 
    } 
} 

如何修改基於這些輸出爲我的隊列我的toString:

我想從一個初始字符串開始顯示汽車的數量 如果隊列爲空,那麼我將返回初始字符串。 如果前面是,或在前面,返回 我想爲前面的每輛車添加一條線到末尾 然後爲那些從陣列的開頭添加線到後面的-1 否則我想添加一條線爲前後車之間的每輛車1。

這是我目前的toString():

public String toString() 
    { 
     String result = ""; 

     for (int scan=0; scan < rear; scan++) 
     result = result + queue[scan].toString() + "\n"; 

     return result; 
    } 

回答

1

你寫的僞代碼,幾if/else-if塊做休息。

public String toString() 
    { 
    String result = String.format("There are %d items in the queue.", count); 
    if (count == 0) 
    { 
     return result; 
    } 
    else if (front >= back) 
    { 
     for (int i = front; i < count; i++) 
     { 
     result += queue[i].toString() + "\n"; 
     } 
     for (int i = 0; i < back; i++) 
     { 
     result += queue[i].toString() + "\n"; 
     } 
    } 
    else 
    { 
     for (int i = front; i < back; i++) 
     { 
     result += queue[i].toString() + "\n"; 
     } 
    } 
    return result; 
    }