手写一个队列容器

最近在处理订单业务中,需要一个队列容器,最开始想使用java提供的Stack,发现很不容易拓展,比如Activity间传递数据或者Activity和Service数据传递,是无法put一个Stack的,所以自己实现一个实现Serializable接口的队列容器,方便数据传递。

不能实例化泛型数组问题

当我在实现过程中,首先遇到的问题是不能写如下代码:

 private final T[] queue;

  public OrderQueue(int size) {
        this.queue = new T[size <= 0 ? 500 : size];
    }

在上述创建过程中会直接报出语法错误提示:

Type parameter 'T' cannot be instantiated directly

解决实例化数组问题

解决方法1:创建一个Object类型数组,然后在获取时转换为T类型

    private final Object[] queue;

    public OrderQueue(int size) {
        this.queue = new Object[size <= 0 ? 500 : size];
    }

    public T getData(int i){
        return (T)this.queue[i];
    }

解决方法2:使用反射机制创建泛型数组

  private final T[] queue;
   public OrderQueue(Class<T> clazz,int size) {
        //利用反射创建数组
        this.queue = (T[]) Array.newInstance(clazz,size);
    }

创建泛型数组在整个实现过程中,遇到的首要问题,我主要想到了以上两种解决办法。

创建OrderQueue 实现 Serializable 接口,方便在Activity或者Service间通信

/**
 * author  : huitao
 * e-mail  : pig.huitao@gmail.com
 * date    : 2021/4/12 21:14
 * desc    :
 * version :
 */
public class OrderQueue<T> implements Serializable {
    private static final long serialVersionUID = -873109114121357176L;
    private final T[] queue;
    private int head = 0;
    private int tail = 0;
    private int realSize = 0;



    public OrderQueue(Class<T> clazz,int size) {
        //利用反射创建数组
        this.queue = (T[]) Array.newInstance(clazz,size);
    }



    public void addLast(T element) {
        if (this.isFull()) {
            this.removeFirst();
        }

        this.tail = (this.head + this.realSize) % this.queue.length;
        this.queue[this.tail] = element;
        ++this.realSize;
    }

    public T removeFirst() {
        if (this.isEmpty()) {
            throw new NoSuchElementException();
        } else {
            T tempLog = this.queue[this.head];
            this.queue[this.head] = null;
            this.head = (this.head + 1) % this.queue.length;
            --this.realSize;
            return tempLog;
        }
    }

    public int realSize() {
        return this.realSize;
    }

    public boolean isEmpty() {
        return this.realSize() == 0;
    }

    public boolean isFull() {
        return this.realSize() == this.queue.length;
    }

    public void clear() {
        while (!this.isEmpty()) {
            this.removeFirst();
        }

    }

    public T get(int index) {
        if (index >= 0 && index < this.realSize) {
            return this.queue[index];
        } else {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.realSize);
        }
    }

    public int indexOf(T key) {
        if (key == null) {
            return -1;
        } else {
            for (int index = 0; index <= this.realSize() - 1; ++index) {
                if (key.equals(this.queue[index])) {
                    return index;
                }
            }

            return -1;
        }
    }

    public int getHead() {
        return this.head;
    }

    public int getTail() {
        return this.tail;
    }

    public T getLast() {
        return this.queue[this.tail];
    }

    public T getFirst() {
        return this.queue[this.head];
    }
}

总结

当在使用的时候就可以直接传递OrderQueue对象:

  private void test(){
        OrderQueue queue = new OrderQueue(Order.class,500);
        Intent intent = new Intent();
        intent.putExtra("data", queue);
        startActivity(intent);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值