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