95

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?

clayton33
  • 4,176
  • 10
  • 45
  • 64

5 Answers5

54

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.

Kennet
  • 5,736
  • 2
  • 25
  • 24
  • 10
    Good 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
25

For those who don't have time to refactor the code to replace arrays with Collections (for example ArrayList), there is an alternative. Unlike Collections, 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;
}
700 Software
  • 85,281
  • 83
  • 234
  • 341
  • 15
    I 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
22

Use Array list http://developer.android.com/reference/java/util/ArrayList.html

Eby
  • 2,769
  • 4
  • 23
  • 30
  • 38
    No 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
7

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

Chris Stillwell
  • 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
6

You can use LinkedList. It has methods peek, poll and offer.

zawhtut
  • 8,335
  • 5
  • 52
  • 76