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 a handy way to create arrays when you know all the values at creation time.
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
We currently have two array examples
that we've been exploring.
0:01
They're pretty similar,
but slightly different.
0:04
First, we have golf scores.
0:07
We know that
we have a fixed number of holes, 18,
0:09
but we don't know what those scores are at
the moment we create the scores array.
0:12
Our other example,
0:17
the friends going to a movie
theater together, is a little different.
0:18
We actually do know who the friends are
at the creation time.
0:22
When you know all the values of an array,
0:26
you really should use
what is known as an array literal.
0:28
It saves a lot of typing,
and it's actually quite concise.
0:31
You're going to love it.
0:34
Let's take a look.
0:35
Go ahead and get your workspace up
0:38
and running with Jshell.
0:40
The declaration of the array
actually still looks the same.
0:43
So we have an array
of strings called friends.
0:47
What looks different
is the way that we set the values.
0:51
We open up with a curly brace, and then
we type the values separated by commas.
0:54
Note that these are curly braces.
1:00
Other programming languages
tend to represent this part with square
1:01
brackets, but Java uses curly braces.
1:05
That's pretty clear, right?
1:08
It sets each index
in the order of the values here.
1:09
So this is index 0, 1, 2, and so on.
1:13
We can do friends 0
1:18
to get the first one out,
and it'll be Brian.
1:19
We can use the up arrow to go back
1:24
and get Rohald out too.
1:25
Also, what happens is the length
1:29
automatically gets set to
the amount that you passed in.
1:31
Pretty handy, right?
1:34
One thing I wanted to point out
is that declaring
1:36
and initializing on the same line
gives us some extra powers.
1:39
If you declare the array first and don't
initialize it, you need to also
1:42
declare its type. So let's say
I wanted to get snacks from the snack bar.
1:46
We're going to have an array of snack
names and we'll call it snacks.
1:50
I'm not going to initialize it,
just declare it.
1:54
Then later, let's say I want to use it.
1:57
Now one would assume you could just say
snacks equals and list the items.
2:00
Let's say we want nachos,
2:04
Sour Patch Kids,
2:07
Snickers,
and I think we need a large popcorn.
2:10
That sounds good.
2:14
So you would think you could just do this,
but you'll get an illegal
2:16
start of expression error.
2:19
This will definitely happen to you
at some point.
2:21
What's happening here is that on
the one-liner, the type has been inferred.
2:24
Java can figure it out
automatically, but here, not so much.
2:28
Here's the workaround.
2:32
Press the up arrow
to bring the previous line back.
2:33
move the cursor over
2:37
and preface it with a new string array.
2:42
Now it knows the type and it'll work fine.
2:45
This typed
literal style is also a great way
2:48
to pass an array into a method
that takes an array as a parameter.
2:51
You don't need to create a new variable,
2:55
you can just do this with the values
inside.
2:56
That way,
you wouldn't need to create a variable
2:59
and it makes an anonymous
or unnamed array for you.
3:01
We'll explore this in a bit.
3:04
Ugh, these snacks look so good!
3:06
I guess I should have asked
the others what they wanted...
3:09
Now that we
3:13
know how to declare, initialize,
and access elements of the array,
3:13
we're ready to start
using them more programmatically.
3:18
We have this wonderful populated data
structure all stored in a single variable.
3:21
Why don't we start looping through it
to show off one of its main benefits?
3:26
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