# How to make array values MOVE UP

**URL:** <https://discourse.processing.org/t/how-to-make-array-values-move-up/1184>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [June 23, 2018, 1:49pm UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184 "2018-06-23T13:49:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![WindJammer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/windjammer/32/320_2.png) [@WindJammer](https://discourse.processing.org/u/WindJammer)\
**Post date:** [June 23, 2018, 1:49pm UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/1 "2018-06-23T13:49:19Z")

</div>

How do i make values in an array move up, everytime the array receives a new value?  
I want to have the newest incoming variable always to be in the 1st array slot. The rest should move up to the next slot.

Example:

> [0] 550  
> [1] 240  
> [2] 160  
> [3]

_when a new value (830) will be added_

> [0] 830  
> [1] 550  
> [2] 240  
> [3] 160

---

<div class="post-metadata">

**Author:** ![Carmine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/carmine/32/480_2.png) [@Carmine](https://discourse.processing.org/u/Carmine)\
**Post date:** [June 23, 2018, 2:14pm UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/2 "2018-06-23T14:14:22Z")

</div>

You can use a “for” loop. Start from the end (length -1) and work to the beginning.

---

<div class="post-metadata">

**Author:** ![bmoren](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bmoren/32/68_2.png) [@bmoren](https://discourse.processing.org/u/bmoren)\
**Post date:** [June 23, 2018, 2:30pm UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/3 "2018-06-23T14:30:19Z")

</div>

check out the array append method.

Expands an array by one element and adds data to the new position. The datatype of the element parameter must be the same as the datatype of the array.

[https://processing.org/reference/append\_.html](https://processing.org/reference/append_.html)

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 23, 2018, 4:34pm UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/4 "2018-06-23T16:34:56Z")

</div>

If you are expanding your array, you should consider using `IntList` or `ArrayList`. You can add to the end of the list and access the first element right at the end of the list at the position defined by `IntList.size()-1`.

Kf

---

<div class="post-metadata">

**Author:** ![madscientist](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@madscientist](https://discourse.processing.org/u/madscientist)\
**Post date:** [June 23, 2018, 4:57pm UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/5 "2018-06-23T16:57:57Z")

</div>

See this post.  
[https://forum.processing.org/one/topic/array-pop-push.html](https://forum.processing.org/one/topic/array-pop-push.html)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 23, 2018, 8:34pm UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/6 "2018-06-23T20:34:16Z")

</div>

> [@WindJammer](#):
>
> I want to have the newest incoming variable always to be in the 1st array slot.

Performance-wise, pushing new elements to the head/top is the slowest, b/c it demands that all the others already inside the container to have their indices right-shifted in order to make room! 👺

Therefore, it’s much preferable to push new elements to the tail/bottom of a container, so there’s no index-shifting. 👌

Given those values you’re using are all `int` numbers, best container for them would be the IntList: 🧐

> **[IntList / Reference](https://processing.org/reference/IntList.html)**
>
> Helper class for a list of int values. Lists are designed to have some features of ArrayList, but to maintain the simplicity and efficiency of working with arrays. Functions such as s…

Use its **append()** method in order to push a new `int` value to its tail: 🕶

> **[append() / Reference](https://processing.org/reference/IntList_append_.html)**
>
> Add a new entry to the list.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 23, 2018, 9:19pm UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/7 "2018-06-23T21:19:25Z")

</div>

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

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

1. [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#addFirst(E))
2. [https://Docs.Oracle.com/javase/10/docs/api/java/util/Deque.html#pollLast()](https://Docs.Oracle.com/javase/10/docs/api/java/util/Deque.html#pollLast())

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

```auto
/**
 * 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;
}

```

---

<div class="post-metadata">

**Author:** ![dan850](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dan850](https://discourse.processing.org/u/dan850)\
**Post date:** [June 24, 2018, 10:08am UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/8 "2018-06-24T10:08:37Z")

</div>

@GoToLoop please go easy with the emojis

---

<div class="post-metadata">

**Author:** ![WindJammer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/windjammer/32/320_2.png) [@WindJammer](https://discourse.processing.org/u/WindJammer)\
**Post date:** [June 24, 2018, 10:19am UTC](https://discourse.processing.org/t/how-to-make-array-values-move-up/1184/9 "2018-06-24T10:19:38Z")

</div>

Looks pretty useful, thanks
