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.
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.
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.
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:
Use its append() method in order to push a new int value to its tail:
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: