Problem statement: 
Design and implement a CIRCULAR buffer for integers. A circular buffer
contains a FIXED number of slots for storing integers and acts like a 
FIFO
queue. New entrants to the queue are added at the END of the buffer. 
Delete
removes from the HEAD of the queue. Implement operations to add, 
delete,
compare (comparing two such queue objects), and find. Make the code 
robust
and consider edge cases. Assume that CONCURRENT access to the buffer is
possible. Please implement it in Java.

解决方案 »

  1.   

    不用实现,有现成的,LinkedHashMap
      

  2.   

    I can't understand so complicated English
      

  3.   

    import java.util.*;
    class FIFOCache 
    {
    private Map map;
    public static int DEFAULT_SIZE=100;
    public FIFOCache(){
    this(DEFAULT_SIZE);
    }
    public FIFOCache(final int cacheSize){
    map=new LinkedHashMap(){
    protected boolean removeEldestEntry(Map.Entry eldest) {
    return size() > cacheSize;
       }
    };
    } public void queue(Object o){
    map.put(o,null);
    }
    public void remove(){
    if(map.isEmpty()) return;
    map.remove(map.keySet().iterator().next());
    }
    public boolean find(Object o){
    return map.containsKey(o);
    }
    public boolean equals(Object o){
    if(o instanceof FIFOCache){
    return map.equals(((FIFOCache)o).map);
    }
    return false;
    } public int hashCode(){
    return map.hashCode();
    }}
      

  4.   

    private int first;
             private int end;
             private int size;
    public void addInteger(Integer e)
    {
    size ++;
    if(size>FIXED_SLOT)
    {
    throw new  OutOfCircularQueueSizeException();
    }
                      end = (end+1)%FIXED_SLOT;
    array[end] = e;
    }
    public Integer delete()
    {
    size --;
    if(size<0) throw new IllegalOperationException();
                      Integer iReturn =  array[(first)%FIXED_SLOT];
                      first = (first+1)%FIXED_SLOT;
                      return iReturn;
    }写了一片段,如果要同步的华,加上synchronized
    也没有测试