How to make array values MOVE UP

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: :stuck_out_tongue:

  1. https://Docs.Oracle.com/javase/10/docs/api/java/util/ArrayDeque.html
  2. 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: :money_mouth_face:

  1. https://Docs.Oracle.com/javase/10/docs/api/java/util/Deque.html#addFirst(E)
  2. https://Docs.Oracle.com/javase/10/docs/api/java/util/Deque.html#pollLast()

Here’s a demo I just did for it: :cowboy_hat_face:

/**
 * 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;
}
3 Likes