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 set and retrieve items from an array.
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
So if your workspace
0:03
timed out, you might not be able
to access your JShell history.
0:04
Normally you can do that
by just using the up arrow.
0:07
Let's go ahead and practice
building our friends array all over again.
0:09
I want to say the type is a string array
0:14
named friends.
0:16
And I want a new string array
0:19
with three elements, right?
0:20
And there we go.
0:24
We have an array with each element
set to the type's default value.
0:25
And we want to set this
0:29
first value here, this first null,
we want to set that to Brian.
0:30
So the way to access
an element of an array is by using
0:34
what is known as an index.
0:37
Now one of the more challenging things
0:39
for you to remember
is that arrays are zero-based,
0:41
and that means that their indexes,
or indices, start with zero.
0:44
So this first element's
index is actually zero.
0:48
So here, let's set the first element
to Brian.
0:52
First, we mention the array, so friends,
and to specify which element
0:55
we are referring to, we can add a square
bracket followed by the index.
1:00
We want to set the first one which is zero
1:04
because counting starts at zeros
with arrays so we going to say Brian
1:07
There we go.
1:15
And now if we take a look at our array
here, we can just type it here,
1:16
it will return what the value looks like. We'll
see that Brian's on the first element there.
1:19
Now I realize that it's weird
that the first element is zero.
1:25
But if you think about it,
we kind of do that
1:28
in English too. Take a moment
and think about how we report on age.
1:30
Currently, my son is in his first year
of life, like before his first birthday.
1:34
So right now, we talk about
how old he is in terms of months.
1:39
It was never like, hey, how old is this cutie?
1:42
Zero.
1:44
No, but it really is his zeroth year.
1:45
Now after he turns one,
we'll just say one, and then two.
1:48
We don't ever really say that,
1:52
but to access that first year of life
by index, I would use zero too.
1:53
Now as weird as it sounds in baby
years, it's okay to say zeroth
1:57
in this array world,
no matter how awkward it sounds.
2:01
This starting at 0
2:04
is such a confusing thing to remember,
and it's the cause of a lot of bugs.
2:05
Now I'll do my best to hammer this home.
2:09
And by the end of this course,
2:11
I hope to have said it enough to make sure
that it comes as second nature.
2:12
So I guess that'd be index
1 in the old "nature array".
2:16
Okay, so the
2:20
next friend in our array
we want to add is Rohald.
2:20
So he would be the second element
in the array. So that would be index one.
2:24
So we say friends one
2:28
equals Rohald.
2:32
There we go.
2:37
And now if we take a look at friends here
we say that we have Brian
2:38
and Rohald. And we can access those values
using the same indexing methods.
2:42
So if I say system.out.println,
and we're going to go ahead
2:46
and say friends 0 is awesome.
2:50
We're accessing the values
using the same indexing.
2:54
And if I use the up arrow,
3:00
we can replace this with 1.
3:01
Rohald is awesome, which is also true.
3:05
I wonder what happens if we give it
an index that doesn't exist in our array.
3:10
Now we only have three elements,
3:14
so what would happen
if I came in here and made this a four?
3:15
Array index out of bounds exception.
3:20
So how do you know what the upper
bound of an array is?
3:24
Well, arrays have a handy
public attribute, which is final,
3:27
and it's named length.
3:30
So if I say friends.length,
I'll see that I have three.
3:32
So you always want to make sure that
your index is one less than the length
3:36
So let do this, I'm going to clear the
screen again, that's control L.
3:40
Let's set the third element to Laura.
3:45
Well actually, why don't you try it yourself?
3:47
Why don't you set the third
element of the friends array to Laura?
3:50
Now go ahead and pause me,
and after you get it, unpause me,
3:54
and I'll show you how I did it.
3:57
Okay, did you fall for it?
4:00
The third element is actually index
2, right?
4:02
So we say friends two equals Laura.
4:05
The last element in our friends
is that index two.
4:12
Therefore, it's one less than the length,
which remember was three.
4:15
So you can always get the last element
in an array by subtracting
4:19
one from its length.
4:23
Now you can actually put expressions
in our brackets as well.
4:25
So I can say friends,
4:27
friends dot length minus one.
4:30
And what will happen is it'll calculate it
and we'll get the value out.
4:33
Now in this example, I already knew which
of my friends were coming along with me.
4:37
So assignment like
this, by index seems, kind of silly.
4:41
It feels like I should just be able to set that
when I create my array, right?
4:45
Well good news, you can.
4:49
It's a bit of a shortcut
and it's known as an array literal.
4:50
Let's look at that
right after this quick break.
4:54
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