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
An array's length is immutable, meaning it cannot be changed. If you want to add or remove, you need to declare a new array and copy into it. Don't worry you won't be doing this too much.
Learn More
Example ArrayList code
// This is coming from the Java Collection Framework
import java.util.List;
import java.util.ArrayList;
// This is using Generic syntax, we'll get to it...
List<String> friends = new ArrayList<>();
friends.add("Brian"); // adds "Brian" to the friends list
friends.size(); // returns 1
friends.contains("Brian"); // returns true
friends.remove("Brian");
friends.size() // returns 0
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
Now that we've seen the
0:05
power of arrays, it's time
to look at some of their limitations.
0:06
Have we been dealing with arrays
where we either know all of the items
0:09
at declaration time, or at least know
how many will be there eventually.
0:13
In reality,
that's not always the case, is it?
0:17
I'm sure you can imagine programs
where we're taking input from a user,
0:20
and they want to either add
or remove elements from the array.
0:25
If you recall,
when we first started talking about this
0:28
fancy new data structure, we talked
about how its length is immutable.
0:31
Now remember,
that means it can't be changed.
0:35
The reason for this immutability
was because the declaration
0:38
required all of the elements to be one
after the other.
0:42
You know, contiguous.
0:45
That brings up quite a big gotcha.
0:47
In order to add an element,
0:50
you would in fact
have to change the length of the array.
0:51
And you can't.
0:54
So what do you do?
0:55
Well, the answer is you
copy that array into a brand new one
0:57
that includes the needed space
for your new element.
1:00
In fact, I have a pretty good example
of how this can creep up.
1:03
In our list of friends,
I forgot to add myself.
1:07
Let's take a look at how to handle this
common gotcha.
1:11
Okay, I'm going to do this in JShell,
so I'm going to use that scratch file.
1:15
And I'll get JShell up and running.
1:19
I'm going to grab this
1:24
string friends array here from explore,
1:25
copy that,
1:29
drop our friends array in there and save
it, because it's a scratch file right?
1:31
Doesn't really matter.
1:34
You can put whatever you want in there.
1:36
We can open it up and use it later.
That's the idea, we explore around.
1:37
Let's get that file in there.
1:42
We'll say open scratch.java,
and then we'll type friends.
1:44
Awesome. So the goal is to add me
to the end of this list.
1:50
There are a couple of approaches.
1:54
The first thing you do is create
your array to what size you want.
1:56
Let's do that.
1:59
It's going to be a string array called
2:01
friends and me.
2:03
We'll do equals new String,
2:09
and we want one more than before, because
I'm going to come to this party too.
2:11
So now there's four.
2:15
There we go.
2:17
You'll notice that it defaulted all here
to null, which is the default for objects.
2:18
And what we want to do is copy
all the elements from our original array
2:23
into this array.
2:27
The method itself is a bit on the older
side of Java.
2:29
It's been around forever,
and it's dangling off of a system class.
2:31
It's called arraycopy.
2:35
If I just start typing ARR and
2:38
I press Tab,
it will go ahead and fill it out.
2:40
One thing that you might not know about
JShell, and I really about this JShell,
2:43
is if I press Tab again,
it will actually show the documentation.
2:47
The first tab shows the signature,
2:51
the second time
it's going to show the documentation.
2:53
It shows the method declaration,
which is important,
2:56
because we need to know
what these signatures here are.
2:58
We need to know about this here, and what
the names of these are, what they mean.
3:01
They're not the greatest, but okay.
3:05
So src is the
source, it's what we're trying to copy.
3:08
So that's our original array, friends.
3:11
All right
source pos that the position, right?
3:17
So that's the starting index
of where we wanna start copying
3:21
from. The position of that is 0 because
3:24
we want to start at the very first element.
3:27
The next parameter is the destination
array.
3:30
The destination array that we want
is the one that we just created.
3:33
We want to copy that to friends and me.
3:37
The next is the dest pause,
so the destination position.
3:40
Where do you want to place that to?
3:44
Since we're just copying over,
we want me at the end, we'll start at 0.
3:46
Finally, the length.
3:51
How many of those items from the first one
do you want to copy over?
3:52
I'm just going to use friends.length,
saying that we want to copy them all.
3:56
Now if we take a look at friends
and me, we'll see that
4:02
we have an extra space at the end
where we can put me.
4:05
I want to put myself
and the last one there.
4:08
Let's say friends and me, 3,
4:10
equals Travis.
4:15
That's quite a bit to remember.
4:18
There's actually another approach
that's a little bit better,
4:19
and it's really similar
to what we just did.
4:22
There's a helper class,
which provides a lot more helpful methods.
4:25
However, it's not readily available
like our system one was.
4:29
We need to actually import the class
to use it.
4:32
If importing is new to you,
check the teacher's notes.
4:35
For right now, let's just do this line.
4:38
Let's do an import here.
4:40
I'm going to say import java.util.arrays.
4:42
That brings this class into our scope.
4:46
And there's a static helper
method off of this class
4:49
called copy of,
and it makes a copy of your array.
4:51
Let's just use it.
4:55
So a string array
called friends and me too,
4:56
and we'll access this arrays.
5:00
Off of that we access
copyOf. Copy of, this time, is camel case.
5:03
It takes the source array which is friends
5:10
and you tell it
5:13
how long you want the new array to be.
This is a little bit different.
5:13
We're going to say that we want
the new array to be friends dot length,
5:17
and we want to be one more, right?
5:21
So plus one.
5:23
There you go.
5:26
It's basically the same thing,
just a little bit more succinct.
5:27
The important thing to recognize here
is that in order
5:31
to make a change to an array,
you need to manage the copying of it.
5:33
If you want to remove an element,
you basically just don't copy it
5:37
over to your new copy of the array,
essentially removing it.
5:40
Check the teacher's notes for more.
5:44
How'd that feel?
5:46
A little gross, right?
5:48
Well, the good news,
there's a solution to this.
5:49
As the old saying goes, if it hurts
when you do that, don't do that.
5:52
When using arrays, you
5:57
actually very rarely ever do any adding
or removing of elements.
5:58
This is because, in practice, there is
another data structure known as a list.
6:03
It is intended for dynamically adding
and removing values.
6:08
Lists are out of the scope of this course,
but we'll get to them shortly
6:12
in an upcoming one, so hold tight.
6:15
And don't stress too much on the mechanics
of adding and removing.
6:18
It's super easy to do as a list.
6:21
And side note, the most common
type of list is called an array list.
6:23
What you just learned
how to do is exactly what's
6:28
abstracted away from you
for adding and removing items.
6:30
It manages all the array growing and
shrinking, and it does all the copying.
6:34
If you just can't wait,
check the teacher's notes for more info.
6:39
Now that
I've bummed you out a bit about arrays,
6:43
let me bring back your appreciation
for them
6:46
by showing off how you can control
their order by sorting.
6:48
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