"How to Make a Website" was retired on March 24, 2020. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Arrays!
You have completed Java Arrays!
Preview
Let's explore how to loop through all elements in the array using a for each approach.
This video doesn't have any notes.
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
[MUSIC]
0:00
So now we have a single variable
that can store multiple elements.
0:04
We can pass that variable around, store it
as a field on an object, but then what?
0:08
In your coding journey, you'll find
that one of the most common tasks in
0:12
programming involves
looping through your data.
0:16
You'll process each and
every value in your data.
0:18
Arrays are wonderful to use for loops.
0:21
So let's go get familiar with them.
0:24
All right, so let's get coding.
0:26
I have attached a new work space here and
there is a file called Explore.java.
0:28
It's got the main boiler plate
0:33
Java application program
already written for you.
0:36
You're welcome.
0:39
So let's take our same
movie theater example
0:40
of getting all these friends together.
0:43
Let's imagine for a second that group
texting doesn't exist because I'm old, and
0:45
I still use one of those flip phones.
0:49
But I wanna send everyone
a text with the movie showtime.
0:51
So let's do this,
let's recreate that friends array here.
0:54
Well, actually, why don't you do it?
0:58
Why don't you use the shortcut method
that you can do when declaring and
1:00
initializing in a single line.
1:04
Here, let's do this,
I'll type out the instructions, let's see.
1:06
Create a new friends array using
the array literal shortcut.
1:10
Include Ben, Alena, and Pasan.
1:18
Okay, pause me, do it and
then unpause me to review how I did it.
1:26
Ready?
1:30
How did you do?
1:32
Here's what I did.
1:33
Made a new string array called friends and
1:35
I added Ben, Alena and Pasan.
1:41
So we want to basically write
a personalized text message to each person
1:49
in our friends array.
1:53
We'll just write out to the console,
but you get the idea.
1:55
Would you wanna process every element in
the array, a great solution to reach for
1:58
is the enhanced for loop.
2:03
The way the enhanced for
loop works is pretty straight forward, so
2:05
first you type the keyword, for.
2:09
Now much like if statements, for
2:12
statements are followed by an expression
which is surrounded by parenthesis.
2:14
And then after you've defined
your expression, again,
2:19
much like an if statement, you open your
block of code with a curly brace and
2:22
close it with a closing curly brace.
2:26
In the enhanced for form,
the expression is pretty clean and clear.
2:29
So what you do first is you declare a new
2:34
variable that you would like
to use within your code block.
2:37
So we're gonna call that String friend.
2:41
And then we're gonna prep the colon and
then friends.
2:45
So this can be read as for
each friend in friends.
2:51
Processing each element of a collection
like this is known as iterating.
2:57
And we are iterating
over our friends array.
3:01
An array is able to be iterated on.
3:05
Or more commonly stated,
arrays are iterable.
3:07
The way that the enhanced for loop works
is that anything that you place over here
3:11
on the right side must be iterable.
3:16
We'll get into how to ensure
that in a future course but for
3:19
now just know that arrays meet
that criteria, they are iterable.
3:23
So, now that we have each element
in the array available on each
3:27
iteration though this loop, we can use it.
3:32
So let's send our pretend text
to each of our friends so,
3:35
let's just use a formatted string, right?
3:40
So let's say System.out.printf,
3:43
let's see, hey %s, the movie starts at 7.
3:49
And remember these flip phones
are hard to type on, so
3:55
I'm just gonna do a C, and a U, and there.
3:58
All right, and
then we're gonna do a new line, %n, cool.
4:00
I'm gonna pass in friend, right?
4:04
Cuz that's what we want to
go in where that %s is.
4:10
So again,
4:13
this friend here is being changed
on each iteration through the loop.
4:14
Each and
every string in this array will run.
4:20
Here, so let's run it.
4:22
So what we'll do is we'll
make sure that this is saved.
4:23
And we'll say clear.
4:27
And javac Explore.java,
4:28
don't need to do that clear but
I like to clear the console before hand.
4:32
And java Explore, and
then that way we can use the up arrow.
4:37
There we go.
We should see, there we go, hey Ben,
4:41
hey Alena, hey Pasan.
4:45
Awesome, so this is pretty clear and
concise, isn't it?
4:47
When you need to process all items in an
array, this enhanced for loop is the way
4:52
to go, and it is the recommended approach
because of how clear it is, right?
4:57
Now you probably notice that this
is called the enhanced for loop.
5:03
So what is this unenhanced for loop then?
5:07
Well, it's just called a for loop,
and let's take a look at that next.
5:10
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