Learn Java for Android Development: Working with Arrays

This post is part of a series called Learn Java for Android Development.
Learn Java for Android Development: Checking Object Type with Instanceof
Learn Java for Android Development: Reflection Basics

This quick lesson shows you how to work with arrays in Java. This lesson is part of an ongoing series of tutorials for developers learning Java in order to develop Android applications.

What is an Array?

An array is a common data structure used to store an ordered list of items. The array elements are typed. For example, you could create an array of characters to represent the vowels in the alphabet:

Much like C or C++, Java arrays are indexed numerically on a 0-based system. This means the first element in the array (that is, ‘a’) is at index 0, the second (‘e’) is at index 1, and so on.

Java makes working with arrays easier than many other programming languages. The array itself is an object (of type array), with all the benefits thereof. For example, you can always check the size of an array using its length property:

What Can I Store In An Array?

You can store any object or primitive type in an array. For example, you can store integers in an array:

Or, you could store non-primitive types like Strings (or any other class) in an array:

Sometimes, you may want to store objects of different types in an array. You can always take advantage of inheritance and use a parent class for the array type. For example, the Object class is the mother of all classes… so you could store different types in a single array like this:

The elements of a Java object array are references (or handles) to objects, not actual instances of objects. An element value is null until it is assigned a valid instance of an object (that is, the array is initialized automatically but you are responsible for assigning its values).

Declaring Arrays

There are a number of ways to declare an array in Java. As you’ve seen, you can declare an array and immediately provide its elements using the C-style squiggly bracket syntax. For example, the following Java code declares an array of integers of length 3 and initializes the array all in one line:

You can also declare an array of a specific size and then assign the value of each element individually, like this:

This is equivalent to creating an array like this:

There are several other ways to create arrays. For example, you can create the array variable and assign it separately using the new keyword. You can also put the array brackets before the variable name, if you desire (this is a style issue). For example, the following Java code defines an array of String elements and then assigns them individually:

Modifying Array Content

As you have seen, you can assign array values by using the bracket syntax:

You can retrieve array values by index as well. For example, you could access the second element in the array called aStopLightColors (defined in the previous section) as follows:

Iterating Arrays

Finally, arrays are often used as an ordered list of objects. Therefore, you may find that you want to iterate through the array in order, accessing each element methodically.

There are a number of ways to do this in Java. Because you can always check the size of an array programmatically, you can use any of the typical for or while loop methods you may find familiar. For example, the following Java code declares a simple integer array of three numbers and uses a simple for-loop to iterate through the items: