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's in Chapter 12/Simple Array Example/MainActivity.java.

Create a project with an empty Activity. Call the project Simple Array Example. Leave the Activity with the default name of MainActivity. It doesn't really matter because we won't be returning to this project.

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 window. Add this code to the onCreate method just after the call to super.onCreate().

// 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 type int
// The indexes are not arbitrary. 0 through 4 or crash!

ourArray[0] = 25;
ourArray[1] = 50;
ourArray[2] = 125;
ourArray[3] = 68;
ourArray[4] = 47;

// Output all the stored values
Log.d("info", "Here is ourArray:");
Log.d("info", "[0] = "+ ourArray[0]);
Log.d("info", "[1] = "+ ourArray[1]);
Log.d("info", "[2] = "+ ourArray[2]);
Log.d("info", "[3] = "+ ourArray[3]);
Log.d("info", "[4] = "+ ourArray[4]);

Next, we add each of the elements of the array together, just as we could regular int type variables. Notice that when we add the array elements together we are doing so over multiple lines. This is fine as we have omitted a semicolon until the last operation, so the Java compiler treats the lines as one statement. Add the code we have just discussed to MainActivity.java.

/*
   We can do any calculation with an array element
   if it is appropriate to the contained type
   Like this:
*/
int answer = ourArray[0] +
ourArray[1] +
ourArray[2] +
ourArray[3] +
ourArray[4];

Log.d("info", "Answer = "+ answer);

Run the example and see the output in the logcat window.

Remember that nothing will happen on the emulator display (but you need an emulator to run the app on) as we send all the output to our logcat window in Android Studio. Here is the output:

info﹕ Here is ourArray:
info﹕ [0] = 25
info﹕ [1] = 50
info﹕ [2] = 125
info﹕ [3] = 68
info﹕ [4] = 47
info﹕ Answer = 315 

We declared an array called ourArray to hold int variables, then allocated space for up to 5 of that type.

Next, we assigned a value to each of the five spaces in our array. Remember that the first space is ourArray[0] and the last space is ourArray[4].

Next, we simply printed the value in each array location to the console and from the output, we can see they hold the value we initialized them to be in the earlier step. Then we added together each of the elements in ourArray and initialized their value to the answer variable. We then printed answer to the logcat window and we can see that indeed all the values were added together, just as if they were plain old int types, which they are, just stored in a different manner.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.139.188