Simple array example mini-app

Let's make a simple working array example. You can get the completed code for this example in the downloadable code bundle. It can be found in the Chapter 15/Simple Array Example/MainActivity.java folder.

Create a project with an Empty Activity and call it Simple Array Example.

First we declare our array, allocate five spaces, and initialize values to each of the elements. Then we output each of the values to the logcat console. Add the following code to the onCreate method just after the call to setContentView:

// Declaring an array int[] ourArray; // Allocate memory for a maximum size of 5 elements ourArray = new int[5]; // Initialize ourArray with values // The values are arbitrary, but they must be int // The indexes ...

Get Android Programming for Beginners - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.