However, if you don’t mind the performance hit, and prefer to keep on pushing new values to the head/top, you should instead go w/ the container ArrayDeque, and use it as a Deque:
- https://Docs.Oracle.com/javase/10/docs/api/java/util/ArrayDeque.html
- https://Docs.Oracle.com/javase/10/docs/api/java/util/Deque.html
Use its method addFirst() to push to the head/top and pollLast() to pop from the tail/bottom:
- https://Docs.Oracle.com/javase/10/docs/api/java/util/Deque.html#addFirst(E)
- https://Docs.Oracle.com/javase/10/docs/api/java/util/Deque.html#pollLast()
Here’s a demo I just did for it:
/**
* ArrayDeque's addFirst() (v1.0)
* GoToLoop (2018/Jun/23)
*
* https://Discourse.Processing.org/t/
* how-to-make-array-values-move-up/1184/7
*/
import java.util.Deque;
import java.util.ArrayDeque;
final Deque<Integer> ints = new ArrayDeque<Integer>();
void setup() {
noLoop();
}
void draw() {
background((color) random(#000000));
println(ints, ENTER);
}
void mousePressed() {
if (mouseButton == LEFT) ints.addFirst((int) random(1000));
else println("Removed oldest:", ints.pollLast());
redraw = true;
}