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