我需要向ArrayList队列添加元素,但当我调用函数添加一个元素时,我希望它在数组的开头添加元素(因此它有最低的索引),如果数组有10个元素,添加一个新的结果是删除最古老的元素(具有最高索引的元素)。
有人有什么建议吗?
我需要向ArrayList队列添加元素,但当我调用函数添加一个元素时,我希望它在数组的开头添加元素(因此它有最低的索引),如果数组有10个元素,添加一个新的结果是删除最古老的元素(具有最高索引的元素)。
有人有什么建议吗?
当前回答
你可能想看看Deque。它使您可以直接访问列表中的第一个和最后一个项。
其他回答
您所描述的是使用Queue的合适情况。
因为你想添加新的元素,并删除旧的。你可以在结尾添加,也可以从开头删除。那不会有太大区别。
Queue有add(e)和remove()方法,它们分别在结尾添加新元素,并从开头删除旧元素。
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(5);
queue.add(6);
queue.remove(); // Remove 5
因此,每次向队列添加元素时,都可以使用remove方法调用将其备份。
更新:
如果你想修复队列的大小,那么你可以看看:- ApacheCommons#CircularFifoBuffer
来自文档:-
CircularFifoBuffer是一个固定大小的先进先出缓冲区 如果已满,则替换最老的元素。
Buffer queue = new CircularFifoBuffer(2); // Max size
queue.add(5);
queue.add(6);
queue.add(7); // Automatically removes the first element `5`
如您所见,当达到最大大小时,添加新元素将自动删除插入的第一个元素。
import com.google.common.collect.Lists;
import java.util.List;
/**
* @author Ciccotta Andrea on 06/11/2020.
*/
public class CollectionUtils {
/**
* It models the prepend O(1), used against the common append/add O(n)
* @param head first element of the list
* @param body rest of the elements of the list
* @return new list (with different memory-reference) made by [head, ...body]
*/
public static <E> List<Object> prepend(final E head, List<E> final body){
return Lists.asList(head, body.toArray());
}
/**
* it models the typed version of prepend(E head, List<E> body)
* @param type the array into which the elements of this list are to be stored
*/
public static <E> List<E> prepend(final E head, List<E> body, final E[] type){
return Lists.asList(head, body.toArray(type));
}
}
带FIFO规则的有界循环缓冲区
看来您需要一个容量有限的循环缓冲区。 您更喜欢类似数组的行为,而不喜欢链接队列。 我说的对吗? 您似乎需要将容量设置为“10”。 这可以通过有界结构实现。 你似乎也需要一个先进先出的纪律。
使用ArrayBlockingQueue的解决方案
一个由数组支持的有界阻塞队列…排序元素FIFO(先进先出)。队列的头是在队列上停留时间最长的元素。队列的尾部是在队列上停留时间最短的元素。在队列尾部插入新元素,队列检索操作获取队列头部的元素。 放,拿都挡。提议,投票不阻拦。
PriorityBlockingQueue
你可以看看PriorityBlockingQueue的文档。有一个类的例子,它“对可比元素应用先进先出的绑定打破”。
ArrayBlockingQueue的示例
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
for (int i = 10; i < 30; i++) {
if (queue.remainingCapacity() > 0) {
queue.offer(i);
} else {
queue.poll();
queue.offer(i);
}
if (queue.size() > 9) {
System.out.println(queue);
}
}
我遇到了类似的问题,试图在现有数组的开头添加一个元素,将现有元素向右移动,并丢弃最古老的元素(数组[length-1])。 我的解决方案可能不是很高效,但它符合我的目的。
Method:
updateArray (Element to insert)
- for all the elements of the Array
- start from the end and replace with the one on the left;
- Array [0] <- Element
祝你好运
举个例子:-
List<String> element1 = new ArrayList<>();
element1.add("two");
element1.add("three");
List<String> element2 = new ArrayList<>();
element2.add("one");
element2.addAll(element1);