本文介绍了寻找数据结构维持尺寸和放大器;清除的过程中较旧的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的用例保持最后n列表访问的网址(其中n是一个固定数)。随着新的网址添加到列表中,旧的URL会自动删除(为了保持在n个元素)

Usecase maintain a list of the last n visited URLs (where n is a fix number). As new URLs are added to the list, older urls are removed automatically (in order to keep it at n elements)

要求需要的数据结构来按时间排序(应该没有问题,如果它接受一个比较)。

Requirement The data structure needs to be sorted by time (should be no problem if it accepts a Comparator).

推荐答案

在java中有几种实施的的。它应该是微不足道的继承或扭曲现有的执行和落实add()方法是这样的:

In java there are several implementation of Queue. It should be trivial to inherit or warp an existing implementation and implement the add() method like this:

boolean add(E e) {
  if(q.size()==MAX_SIZE) {
    remove();
  }
  q.add(e)
}

这篇关于寻找数据结构维持尺寸和放大器;清除的过程中较旧的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:37