唯一的方法是维护环形缓冲区。也就是说,您有一个计数器,它记住起始位置,然后移动它而不是移动数组中的所有条目。这仅适用于您重新定义“开始”的含义。
请参阅ArrayDeque的源代码,其中包含三个字段
86 96 private transient E[] elements; 97 98 103 private transient int head; 104 105 109 private transient int tail;
因此,添加到开始像这样
224 public void addFirst(E e) { 225if (e == null) 226 throw new NullPointerException(); 227elements[head = (head - 1) & (elements.length - 1)] = e; 228if (head == tail) 229 doubleCapacity(); 230 } 312 315 public E getFirst() { 316E x = elements[head]; 317if (x == null) 318 throw new NoSuchElementException(); 319return x; 320 }注意:它移动头部,而不是将所有元素向下移动阵列。



