Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
It's often helpful to group items together into an array. In this video, we'll create an array of facts. Then we'll use our randomNumber to help us pick a random fact!
Facts (for copy/paste):
String[] facts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built." };
Further Reading
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We just programmed the heart of our app,
the random number generator.
0:00
Now we need to use that generator
to help us pick a random fact
0:04
on each button click.
0:08
But before we can do that,
we'll need a pool of facts to choose from.
0:09
Let's start by creating
an array to hold our facts.
0:14
Remember, an array is just a container
that holds values of a specific type.
0:17
Let's add two lines to the top
of the onClick method.
0:22
And then type String, facts.
0:27
We now have a new variable called facts,
and
0:33
we've declared it to be an array
of strings, or a string array.
0:35
You can make an array out
of any type of object.
0:40
All you need to do is add square
brackets after the data tag.
0:43
All right, now for
the assignment side of things.
0:47
For the sake of time,
I'm going to paste in the ten facts and
0:50
then we'll go over the syntax.
0:54
You can type these out if you want, but
I've put this code in the teacher's notes
0:56
so you can just copy and
paste as well, let's do it.
0:59
I'll select everything after
the word facts, up to but
1:03
not including the semicolon.
1:07
Then I'll copy this and
paste it between facts and
1:09
the semicolon back in the onClick method.
1:13
And now we can see that one
way to initialize an array
1:16
is with an comma-delimited list
between two curly brackets.
1:19
Now that we have an array
of strings to select from,
1:24
let's use our random number generator
to help us pick one at random.
1:27
Our array has ten elements in it.
1:31
Let's start by changing our
random number generator
1:33
to have a range of 10 instead of 3.
1:36
If we clicked on our button now, we'd
see random numbers ranging from 0 to 9.
1:41
And what else do we have
that ranges from 0 to 9?
1:47
That's right,
the indices of our facts array.
1:49
This first fact about ants is at index 0.
1:52
And this last fact about
mammoths is at index 9.
1:56
So if we want to select a random fact,
we just need to select the fact
2:00
from our facts array that's at the index
of the random number we generated.
2:04
It seems easy enough.
2:09
Let's start by getting rid of what we're
currently setting our fact to, And
2:11
then type facts,
in square brackets, randomNumber,
2:18
to select the fact at
the index of our randomNumber.
2:23
All right, that should do it.
2:29
But before we test our app, there's
a potential bug we should fix first.
2:31
What happens if we add a new
fun fact to the array?
2:35
Can you tell where the code will break?
2:39
I'll add another fact and
then let's talk about it.
2:41
I'll say,
2:43
Treehouse is not actually in a tree.
2:49
Now that we have 11 elements in our array,
2:55
we need to change 10 to 11 down
here in our call to nextInt.
2:58
But if we forget to change it, we'll
never see our new fun fact in the app.
3:02
Wouldn't it be nice if we could somehow
use the number of elements in the array
3:07
as our parameter instead of
a hard-coded number like this?
3:11
Then we wouldn't have to change
it every time that we add or
3:14
remove elements in our array.
3:17
Sure enough, Java has a way for
us to do this.
3:19
Let's delete the number 10 here and
type facts.length.
3:22
This gives us the length
of the facts array,
3:28
which is another way to say
the total number of elements.
3:30
By using this property of the array,
3:33
we will always pass in
the right number of elements.
3:36
This saves us the work of having
to manually change numbers and
3:39
it also protects us from errors.
3:42
For example, if we had deleted an element,
I'll delete this one that I just added,
3:45
But we left this number as 11, then our
app may try to reference an element that
3:54
doesn't exist and it would crash.
3:58
Looks good to me, let's click on
the run button to test our app.
4:01
And then we can tap the button and
we should see some of the new facts.
4:07
And there we go,
4:12
mammoths still walked the earth when
the Great Pyramid was being built.
4:13
And if we keep clicking,
we should eventually see all of our facts,
4:18
very cool.
4:23
Awesome work, don't worry if all these
new Android words aren't sticking yet.
4:26
We've done a lot and
we still have a lot to do.
4:31
But if you stick with it,
I'll explain everything you need to know.
4:34
And if you want to talk about
anything you're stuck on,
4:37
head over to the community for
questions or discussions.
4:40
It's easy to feel
overwhelmed with this stuff.
4:43
But trust me, things will start to
make more sense the more you practice.
4:45
On that note,
why don't you try some more practice?
4:50
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up