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 timed out, you might
not be able to access your jshell history,
0:00
normally you can just do
that by using the up arrow.
0:04
Let's go ahead and practice building
our friend's array all over again.
0:07
So let's see.
0:12
I want to say that the type is
a String array named friends and
0:13
I want a new string array
with three elements, right?
0:20
And there we go,
0:26
we have an array with each element
set to the types of default value.
0:27
So we wanna set this first value,
the first value here, this first null.
0:33
We wanna set that to Pasan, so
0:38
the way to access an element of an array
is by using what is known as an index.
0:40
Now one of the more challenging things for
you to remember is that arrays
0:46
are zero based and that means that their
indexes or indices start with zero.
0:52
So the first elements
index is actually zero.
0:57
So here,
lets set the first element to Pasan.
1:01
So first we mention the array, so friends.
1:04
And to specify which element we
are referring to we can add a hard bracket
1:07
followed by the index, which we want
to set the first one which is zero.
1:12
Cuz counting starts at zero with arrays,
so we are gonna say Pasan.
1:17
There we go, and
now if we take a look at our array here,
1:24
we can just type it here,
it will return what the value looks like.
1:27
We'll see that Pasan's on
the first element there.
1:31
Now, I realize that it's weird
that the first element is zero.
1:34
But if you think about it,
we kinda do that too in English.
1:37
So, take a moment and
think about how we report on age.
1:40
Now my youngest daughter
is about to turn three.
1:44
Her first year of life though,
like before her first birthday,
1:47
we talked about how old she
was in terms of months.
1:50
It was never like,
hey how old is this cutie?
1:53
Zero, no, but
it was really her zeroth year.
1:57
Now after she turned one,
we just say one and now she's two.
2:01
We don't really ever say that,
2:05
but to access that first year of
life by index I would use zero too.
2:07
Now as weird as it sounds in baby
years it's okay to say zeroth in
2:12
this array world no matter
how awkward it sounds.
2:16
This starting at zero is such
a confusing thing to remember and
2:20
its the cause of a lot of bugs.
2:24
Now I'll do my best to hammer this home
and by the end of the course I hope to
2:26
have man you enough about it to make
sure that it comes as a second nature.
2:30
So I guess that'd be index
one in the old nature array.
2:36
Okay, so
next in our array we want to add Alena.
2:40
So she would be the second
element in the array.
2:43
So that would be index 1.
2:46
So we'll say friends[1] is Alena.
2:49
There we go and
I refreshed the screen with Ctrl+L and
2:57
now if we take a look at friends here,
we'll see that we have Pasan and Alena.
3:00
And we can access those values
using the same indexing methods.
3:05
So if I say system.out.print.ln and
3:08
we're gonna go ahead and
3:13
say (friends[0] + is awesome!
3:17
We're accessing the values
using the same indexing.
3:25
And if I use the up arrow
we can replace this with.
3:29
1, Alena is awesome, which is true.
3:36
I wonder what happens if we give it
an index that doesn't exist in our array.
3:41
Now we only have three elements.
3:45
So what would happen if I came in here and
made this a 4?
3:46
ArrayIndexOutOfBoundsException.
3:53
So how do you know what
the upper bound of an array is?
3:56
Well, arrays have a handy public
attribute, which is final, and
3:59
it's named length.
4:03
So if I say friends.length,
I'll see that I have three.
4:05
So you always wanna make sure that
your index is less than the length.
4:10
So let's do this.
4:15
I'm gonna clear the screen again,
that's Ctrl+L.
4:16
So let's do this.
4:18
Let's set the third element to Ben.
4:20
Well, actually,
why don't you try it yourself?
4:22
Why don't you set the third element
4:24
of the friends array to Ben.
4:30
Now go ahead and pause me and
after you get it unpause me and
4:35
I'll show you how I did it.
4:38
So go ahead and pause me.
4:39
Okay, did you fall for it?
4:41
The third element is actually index 2,
right?
4:44
So if we say friends 2 equals Ben, right?
4:47
The last element in our
friends is at index 2,
4:53
therefore it's 1 less than
the link which remember, was 3.
4:56
So you can always get the last element in
an array by subtracting 1 from its length.
5:00
Now you can actually put expressions
in our brackets as well.
5:06
So I can say friends, friends.length which
really is three, we know that minus 1.
5:10
And what will happen is
that we'll calculate and
5:19
then we'll get the value out.
5:21
Now in this example, I already knew which
of my friends were coming along with me.
5:25
So assignment like this,
by index seems kinda silly.
5:29
It feels like I should be able to just
set that when I create my friends
5:33
array, right?
5:36
Well good news, you can.
5:37
It's a bit of a shortcut and
it's known as an array literal.
5:39
Let's look at that right
after this quick break.
5:42
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