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.
Resources
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
So now we have a single variable
that can store multiple elements.
0:05
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
0:14
tasks in programming involves
looping through your data.
0:16
You'll process each and every value in it.
0:20
Now I'm assuming you've completed
the prerequisites for this course.
0:23
In that list is the Java Loops course,
which covers this enhanced for loop.
0:27
But at the time, it's possible
you didn't know about arrays yet.
0:31
So let's quickly recap
the enhanced for loop with your newfound
0:35
array knowledge, and hopefully things
make even more sense this time.
0:38
Alright, so let's get coding.
0:43
In the workspace
here is a file called Explore.java.
0:46
It's got the main boilerplate
Java application program
0:49
already written for you.
0:52
I'm such a nice guy.
0:54
So let's take our same movie theater
example of getting all these friends
0:56
together.
0:59
Let's imagine group texting doesn't exist.
1:00
Let's pretend we back in the early 2000s
but I want to send everyone a text
1:03
with the movie showtime. So let's do this.
Let's recreate that friends array here.
1:07
Well actually, why don't you do it?
Why don't you use that shortcut
1:12
method that you can do when declaring
and initializing in a single line?
1:15
Here, let's do this.
I'll type out the instructions.
1:20
Let's see.
1:22
Create a new friends
array using the array literal shortcut.
1:23
Include Brian, Rohald, and Laura.
1:28
Okay, pause me, do it, and then unpause me
to review how I did it.
1:32
Ready?
1:36
Go ahead.
1:36
How did you do?
1:40
Here's my approach.
1:41
I'll make a new string array
called friends,
1:43
and I'll add Brian,
1:47
Rohald, and Laura in the curly braces.
1:49
Cool.
1:53
So we want to basically write
a personalized
1:54
text message
to each person in our friends array.
1:56
We'll just write it out to the console,
but you get the idea.
1:59
When you want to process every element
in the array, a great solution to reach
2:03
for is the enhanced for loop, or also
commonly referred to as the for each loop.
2:07
The way the enhanced for loop
works is pretty straightforward
2:13
and hopefully you already watched
2:16
the video that I mentioned earlier
so we'll breeze through this quickly here.
2:17
So first you type the keyword for
2:21
then our parentheses for our expression
2:24
and then you open up
your block of code with curly braces.
2:26
So what you do first is you
2:30
declare a new variable that you would
like to use within your code block.
2:31
So we're going to call
that string, friend,
2:35
and then we're going to put a colon,
2:38
and then our array friends
that we want to loop over.
2:40
So this can be read
as for each friend in friends.
2:43
Processing each element of a collection
this is known as iterating,
2:48
and we are iterating over our friends
array.
2:51
An array is able to be iterated on,
or more commonly
2:54
stated, arrays are iterable.
2:57
The way that the enhanced for loop works
is that anything you place over here
3:00
on the right side must be iterable.
3:04
We'll get into how to ensure that
in a future course, but for now,
3:07
just know that arrays meet that criteria.
3:10
They are iterable.
3:12
So now that we have each element
in the array
3:14
available on each iteration
through this loop, we can use it.
3:16
So let's send our pretend text
to each of our friends.
3:20
Let's just use a formatted string, right?
So let's say system.out.printf.
3:23
Let's see... Hey %s,
3:28
the movie starts at 7, see you there!
3:32
All right, and then we're
going to do a new line, %n.
3:36
Cool.
3:39
I'm going to pass in friend, right?
3:40
Because that's what we want to go in
where that %s is.
3:42
So again, this friend here
is being changed on each iteration
3:46
through the loop.
3:50
Each and every string in this array
will run here.
3:51
So let's run it.
3:53
So what we'll do
is we'll make sure that this is saved
3:56
and we'll say clear and javac
3:59
Explore.java, and you
don't need to do that clear, but I
4:02
like to clear the console beforehand
and java Explore.
4:06
And then that
way you can use the up arrow.
4:10
We should see, there we go.
4:14
Hey Brian, hey Rohald, hey Laura.
Awesome!
4:16
So this is pretty clear
and concise, isn't it?
4:19
When you need to process
4:21
all items in an array,
this enhanced for loop is the way to go.
4:22
And it is the recommended approach
because of how clear it is, right?
4:26
Okay, in the next video,
we'll revisit our friend
4:30
the standard for loop
and learn to use it with arrays.
4:32
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