I am trying to add, remove and reference items from an array I create in my main java file, but I am having trouble figuring out the correct syntax. In actionscript they have push() and pop() for adding and removing items in an array, is there an equivalent in android?
5 Answers
In Java an array has a fixed size (after initialisation), meaning that you can't add or remove items from an array.
int[] i = new int[10];
The above snippet mean that the array of integers has a length of 10. It's not possible add an eleventh integer, without re-assign the reference to a new array, like the following:
int[] i = new int[11];
In Java the package java.util contains all kinds of data structures that can handle adding and removing items from array-like collections. The classic data structure Stack has methods for push and pop.

- 5,736
- 2
- 25
- 24
-
10Good explanation, however keep in mind that Stack isn't that great of a class as it inherits from Vector, and as such is fully synchronized. So unless you need the synchronization, ArrayList or LinkedList is likely a better option. – M. Jessup Dec 27 '10 at 15:00
For those who don't have time to refactor the code to replace arrays with Collection
s (for example ArrayList
), there is an alternative. Unlike Collection
s, the length of an array cannot be changed, but the array can be replaced, like this:
array = push(array, item);
The drawbacks are that
- the whole array has to be copied each time you push, and
- the original array
Object
is not changed, so you have to update the variable(s) as appropriate.
Here is the push
method for String
:
(You can create multiple push
methods, one for String
, one for int
, etc)
private static String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
for (int i = 0; i < array.length; i++)
longer[i] = array[i];
longer[array.length] = push;
return longer;
}
This alternative is more efficient, shorter & harder to read:
private static String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
System.arraycopy(array, 0, longer, 0, array.length);
longer[array.length] = push;
return longer;
}

- 85,281
- 83
- 234
- 341
-
15I strongly discourage the use of this method. For really simple and small arrays it won't impose problems, but for a medium or heavy array this will cause the system to iterate over the array every time an item is added. If you have a 100+ items array and want to add more 3 items, it will do 303 operations. – Panthro May 12 '15 at 08:53

- 2,769
- 4
- 23
- 30
-
38No idea why this is the accepted answer. The right answer is java.util.ArrayDeque or java.util.LinkedList – Rotem Aug 09 '15 at 06:32
-
1@Rotem I think you can easily find the reason here: https://stackoverflow.com/questions/8452672/java-howto-arraylist-push-pop-shift-and-unshift – Eddie C. Apr 18 '18 at 13:13
You can use Arrays.copyOf()
with a little reflection to make a nice helper function.
public class ArrayHelper {
public static <T> T[] push(T[] arr, T item) {
T[] tmp = Arrays.copyOf(arr, arr.length + 1);
tmp[tmp.length - 1] = item;
return tmp;
}
public static <T> T[] pop(T[] arr) {
T[] tmp = Arrays.copyOf(arr, arr.length - 1);
return tmp;
}
}
Usage:
String[] items = new String[]{"a", "b", "c"};
items = ArrayHelper.push(items, "d");
items = ArrayHelper.push(items, "e");
items = ArrayHelper.pop(items);
Results
Original: a,b,c
Array after push calls: a,b,c,d,e
Array after pop call: a,b,c,d

- 10,266
- 10
- 67
- 77
-
`Arrays.copyOf()` creates new array each time it's called. If you have to pop quite a long array in a loop, performance will be much worse than `for (...) {String stringC = stringA + stringB}` – Farid Oct 18 '19 at 10:41