Index

Android apps - Arrays


This is all Java. By the way, do NOT try to access past the end of an array (by using a subscript larger than the array size). This will bomb you out of the app when running.

Define array with size
Define two dimensional array
Define array and specify size later size
Define and initialise array
Copying one array to another
Number of elements

Define array with size

public static int [] arrayName = new int [arraySize];

It doesn't have to be "public static" of course. But that means that you can use it in other Java classes, which tend to proliferate in an Android app. You often use arrays in a Grid, for example. To use this array:

arrayName [5] = 18;

Define two dimensional array

public static int [] [] array2 = new int [arrayRows] [arrayColumns];

To use this array:

array2 [5] [4] = 25;

Define array and specify size later size

You may want to define an array as "public static", but change its size dynamically in the program, which will be in a method, and you can't define things as "public static" in a method. Sigh. So define it like this in the "global variables" (outside all the method, such as before the first one):

public static int [] array;

and then in a method somewhere, you say:

array = new int [arraySize];

and you can make "arraySize" what you want, or even do this several times in different places. Distinctly useful.


Define and initialise array

A good way to define an array is to initialise it at the same time, especially if all elements are different, and don't change. For example, this sets up an array of pictures (different coloured blocks, as it happens).

public static int [] colour = { R.drawable.colour0, R.drawable.colour1, R.drawable.colour2, R.drawable.colour3, R.drawable.colour4, R.drawable.colour5, R.drawable.colour6, R.drawable.colour7, R.drawable.colour8, R.drawable.colour9, R.drawable.colour10, R.drawable.colour11, R.drawable.colour12, R.drawable.colour13, R.drawable.colour14, R.drawable.colour15, };

The number of elements is worked out by Java from the number of initialisations - so 16 in this case.

You can do this in two stages (as above), with the "public static" definition outside the methods, and the initialisation done later, inside a method:

public static int[] colour; // and later on, inside a method: colour = new int [] { R.drawable.colour0, R.drawable.colour1, R.drawable.colour2, R.drawable.colour3, R.drawable.colour4, R.drawable.colour5, R.drawable.colour6, R.drawable.colour7, R.drawable.colour8, R.drawable.colour9, R.drawable.colour10, R.drawable.colour11, R.drawable.colour12, R.drawable.colour13, R.drawable.colour14, R.drawable.colour15, };

Copying one array to another

You could set up a loop, but AndroidStudio flags up a warnung (sigh!). It wants you to use this

System.arraycopy(array1, 0, array2,0, length);

This copies "array", starting at element 0, to "array2", starting at element 0, for "length" number of elements.


Number of elements

You can access the number of elements in an array, which can be useful in a "for" loop;

for (int i=0;i<array.length;i++){array[i]=0;};

"array.length" is not the length of the individual elements added together, but just the number of elements.



© Jo Edkins 2016 - Index to Android app index