Java: How to Use an Array in an App

Header

Arrays have a beautiful way of condensing code. To put it simply, an array is just storage space. It’s a closet. You throw data inside, so that it’s easier to find and use later.

To illustrate why closets are the way to go, we’re gonna go back to the Hint button on the Riddler App from last week.

The first time that button got hit, a toast message popped up with a hint, and the second, a different one popped up. Let’s add more.

Three more.

No_Array

That’s getting a little out of hand, right? There’s too much repetition in there. Code is meant to eliminate repetition. Plus it’s just giving me more chances to misplace a semicolon or use = when I mean ==. Typos are 90% of my debugging process, so I’m a big fan of anything that means fewer lines to check.

Here’s where our closet comes in. We’re going to stash all our hints inside the XML file with all our strings, that way the java file isn’t about storage, it’s all about logic.

How do I make a string array in XML?

Go into your Strings.xml file, and scroll to the bottom.  You’ll start off with a tag called string-array name, and then list each string inside <item> tags, like this:

<string-array name="riddles">
    <item>It has no fingers or toes either.</item>
    <item>And lives all over the world.</item>
    <item>You do not know this.</item>
    <item>Please stop pressing hint.</item>
    <item>Knock it off!</item>
</string-array>

To make life confusing, arrays do this fun thing where they start counting at 0, so keep in mind, this array has 5 items, but when you’re scripting, you’ll want to think of them as Item 0, Item 1, Item 2, and so on.

AnXMLArray

Now, go back into the MainActivity.java file and scroll down. To pull the strings from the .xml file into the method, we’ll need to create a java array above the showToast method. I’m going to call this one hints.

public String[] hints;

Pushing the XML array into the java only requires one line. You can plop it inside the showToast method.

hints = getResources().getStringArray (R.array.riddles);

This slideshow requires JavaScript.

With that in place, we can move into the logic.

To extract an item from a java array, you can reference it by position. For our array, we’d call the first item with hints[0].

Fun tip, you don’t have to use a number—you can use a variable to tell Android Studio what you want from the array. We’re going to use the hint_counter variable to loop through several hints before settling on one last piece of toast that the user will get over and over again if they keep hitting the button.

That means the meat of our code is going to look like this:

if (hint_counter<4) {
Toast toast = Toast.makeText(this, hints[hint_counter], Toast.LENGTH_SHORT);
toast.show();
hint_counter++;
} else {
Toast toast = Toast.makeText(this, hints[4], Toast.LENGTH_SHORT);
toast.show();
}

This slideshow requires JavaScript.

And that’s it.

To recap, this accomplishes the same result as the 22 line ToastMethod above, but it’s doing it in 12 lines. Significantly more readable.

Final

Install it, and you’ll get a helpful but kinda rude Riddler app that looks like this:

Result

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s