Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's explore a handy way to create arrays when you know all the values at creation time.
-
0:00
We currently have two array examples that we've been exploring.
-
0:03
They're pretty similar but slightly different.
-
0:06
Now first, we have golf scores.
-
0:07
We know that we have a fix number of holes, 18.
-
0:11
But we don't know what those scores are the moment that we create scores array.
-
0:16
Our other example, the friends going to a movie together.
-
0:19
We actually do note who the friends are at creation time.
-
0:22
Now when you know all the values of an array,
-
0:24
you really should use what is known as an array literate.
-
0:27
It saves a lot of typing and it's actually quite concise.
-
0:30
You're gonna love it.
-
0:31
Let's go take a look.
-
0:32
So go ahead and get your workspace up and running with jshell.
-
0:36
The declaration of the array actually still looks the same.
-
0:40
So we have a String of friends.
-
0:42
Now what looks different is the way that we set the values.
-
0:45
So what we do is we open up with a curly brace, and
-
0:48
let me type in the values separated by comas.
-
0:55
Now, note that these are curly braces other programming languages tend to
-
1:01
represent this bit here as a hard bracket, but Java uses curly braces.
-
1:06
Now, that's pretty clear, isn't it?
-
1:09
So, it sets each index in the order of the values here, right.
-
1:13
So this is 0, 1, 2, right?
-
1:15
So, we can do friends 0, get the first one out, and then it's gonna be Pasan.
-
1:22
And we can use the up arrow, we can look and we can get Alena out too, right.
-
1:27
And also, what happens is it automatically get set,
-
1:31
the length to the amount that you passed in, automatically.
-
1:36
Pretty handy, right?
-
1:37
So one thing I wanted to point out that this declaring and
-
1:40
initializing on the same line, gave us some extra powers.
-
1:44
If you declare that array first and
-
1:46
don't initialize it, you need to also declare its type, so let's do that.
-
1:51
So, let's say that I wanted to get snacks from the snack, so
-
1:54
we're gonna have an array of snack name and so we'll call it snacks.
-
1:58
Okay, and I'm not gonna initialize it, just declare it.
-
2:01
And then later let's say that I wanted to use it.
-
2:03
Now, one would assume that you can probably just say snacks =,
-
2:09
let's see, we wanna gonna get nachos.
-
2:13
We want some Sour Patch Kids, obviously,
-
2:17
probably need that Snickers, and I think we need a large popcorn.
-
2:24
Here we go, that sounds good.
-
2:26
So you would think that you could just do this,
-
2:29
but what you'll see is you'll get this illegal start of expression.
-
2:33
So this'll happen to you for sure.
-
2:34
Now, what's happened here is on the one liner this type has been inferred,
-
2:40
it could figure it out automatically.
-
2:43
Here though not so much, but here's the work around.
-
2:45
So I'm gonna go ahead, I'm gonna press the up arrow.
-
2:49
And I will preface this, move our cursor over here,
-
2:53
we're gonna preface this with a new string array.
-
3:01
Here we go, and now it knows the type and now it will work fine.
-
3:05
So this typed literal style is also a great way
-
3:08
to pass an array into a method that takes an array as a parameter.
-
3:12
This way you don't need to create a new variable, you can just do this.
-
3:15
You can just say new string and then curly braces the values, there you go.
-
3:20
That you way, you wouldn't need to create a variable.
-
3:22
And it makes an anonymous or unnamed array for you.
-
3:26
Well, we'll explore this in a bit.
-
3:28
This next looks so good.
-
3:30
I guess I should've asked the others what they wanted.
-
3:33
So now that we know how to declare, initialize, and
-
3:36
access elements of the array, we are ready to start using them more programmatically.
-
3:41
We have this wonderful populated data structure
-
3:44
now all stored in a single variable.
-
3:45
Why don't we start looping through it showing off one of its main benefits.
You need to sign up for Treehouse in order to download course files.
Sign up