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 Data Structures!
You have completed Java Data Structures!
Preview
Let's take our new List knowledge and add it to our class.
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
Alright,
so let's play around with some lists.
0:01
Sound good?
0:03
Now, please make sure that you've opened
the new workspace
0:05
associated with this video.
0:08
I've made some significant changes to our
movies and example classes to work with,
0:10
and if you're still using
the original workspace,
0:14
you're not going to be able
to follow along.
0:16
Okay,
so I haven't touched our movie class.
0:20
We still have this unused mdirector
variable, and we've got a serial version
0:22
ID in place.
0:26
I've removed our ser file,
and in example.java,
0:27
I've removed all of the code
except for our load and print statement.
0:32
Let's go to movies.java.
0:36
Here, I've imported arrays and local date
and moved our instance creations
0:38
down into another static method here,
just to keep our example file cleaner
0:43
and separate this out a bit.
0:47
I've added some
0:50
movies in so you wouldn't have to type
or copy and paste all this.
0:50
Notice though, I'm using arrays.asList
for our new director argument.
0:54
Some movies have more than one director
and they should all get recognized.
0:59
So this is essentially what we had
before though.
1:03
We're creating a bunch of movie instances,
adding them to an array, sorting it,
1:05
and passing them on to our save method
above to serialize them.
1:10
If we take a look back at our movie class,
currently
1:14
our mdirector variable is just a string.
1:17
So let's update our movie class
to take in a list of strings here.
1:20
Let's import java.util.list up top here.
1:24
Cool.
1:32
Now let's change
this mDirector from a string type
1:33
to a list of strings type
and rename it to Directors, plural.
1:36
Okay, let's add it to our constructor
now too.
1:44
We'll pop in a list of strings
in the second position
1:47
and name it Directors.
1:50
Then let's set it inside.
1:55
Looking good,
2:02
let's create our getter method too.
2:03
So public
this will return a list of strings
2:09
Get Directors
2:13
And we return mDirectors. Awesome.
2:18
You know what?
Let also add it to our toString method.
2:23
We'll say
2:31
directed by %s %n
2:31
and add in mdirectors there.
2:38
Alrighty we should be all set.
2:46
Back in example let's call
this new method to serialize our movies
2:49
and make sure that we have everything
set up correctly.
2:53
That method was named serializeMovieList.
2:56
I'm just going to copy it here.
2:59
In example,
we'll say movies.serializeMovieList.
3:02
Okay let's run
3:06
this file and let's
pipe it into less like we've done before.
3:10
Cross your fingers.
3:28
All right, we're in business.
3:31
Using the down arrow, we'll scroll down,
or the space bar will jump sections
3:33
for you.
3:37
Awesome.
3:39
We got all of our movies
with our list of directors now, too.
3:39
And they've been serialized. Nice job.
3:43
I'll hit Q to get out of this.
3:46
And we can comment out this save line now.
3:51
Perfect.
3:54
So we have our loaded movies array here,
which is fine.
3:56
We can leave that.
3:58
If we need to do anything
other than read it at some point,
3:59
we can easily change this to a list,
no problem.
4:02
So what I would like to do
is print out all of the directors,
4:05
because I'm a nerd like that.
4:08
So if this were more of a real-world
situation,
4:10
where this data is in a database
or coming from an API,
4:13
we wouldn't be able to check how many
directors we're going to be getting,
4:17
because most have one,
but others have multiple.
4:21
We wouldn't know what length
to declare an array at, so
4:24
using a list is the way to go here.
4:27
So let's make a list
4:32
of strings named all directors
4:33
equals a new array list of strings.
4:37
So we need to loop over an array
and get our directors and loop over that.
4:43
Sounds a nested loop situation again.
4:47
Let's do it.
4:50
Oh look at that,
we're already looping over our array.
4:52
We're halfway there.
4:54
Let's remove this print line
and since we're only working
4:56
with these loaded movies
now I'm just going to rename this to movie
4:59
to keep things readable.
5:02
All right for each movie
we need to get our directors.
5:06
We have our get directors getter method
which returns a list so let's store
5:09
that returned value in a variable.
We'll say list of strings
5:14
directors equals
5:19
movie dot get directors.
5:22
Now we want to loop over
this new directors list, so let's say for
5:27
string director in directors.
5:33
Now, just so you know,
I typically to take the extra step
5:37
and store this in a variable
when I'm learning new things.
5:40
To me, it's a bit easier to read
and understand later on
5:44
if I'm referring back to this stuff
because I forgot how to do something.
5:46
But you could easily just run this
for loop on movie.getdirectors here.
5:52
That's up to you, but I'm okay
with having one more line in my code
5:55
to help me out later on.
5:58
Alright, let's just add
6:03
each director to our new fancy list, huh?
6:04
All directors.add director.
6:08
That's a lot of director talk.
6:10
Director, directors, all directors.
6:12
I apologize
if that made your head spin a bit.
6:15
But basically, we've created a new list
to store all of our directors.
6:17
To get them,
we needed to loop over each movie.
6:22
and on each movie,
loop over that movie's directors.
6:25
Then for each director
in that second loop, add it to our list.
6:29
Nice.
6:33
So it
won't look pretty without a loop again,
6:33
but let's just print out our all directors
list to make sure we did it
6:35
right.
6:39
Oops, what did I do now?
6:51
Oh I forgot to reimport these things.
Let's go import
6:53
list and ArrayList.
6:57
Okay, one more time.
7:17
All right, we did it.
7:24
Not only did we incorporate a list
into our class, we also created a new one
7:25
filled with all of the directors
in our list of movies,
7:30
no matter how many directors
worked on the movie.
7:33
Great job here!
7:35
Though I do notice some duplicates here.
7:37
We've got two Spielbergs,
a few Miyazakis, two Nolans.
7:39
Hmm, that's not very useful, is it?
7:42
Don't worry, in the next video
we'll learn an easy way to handle this.
7:46
See? So much
7:51
better than arrays,
and they're super easy to use.
7:52
You'll encounter
these all over the place, and used in
7:56
just about any external library
that you encounter.
7:59
One thing we should discuss here
real quick is object references.
8:02
This gets people confused sometimes
when just getting started in Java,
8:07
so let's just get this out of the way.
8:10
When we add objects
to a collection, arrays included,
8:13
we are actually adding
the object reference.
8:16
Now remember, a reference is used to point
to a specific place in memory,
8:19
so let's discuss it this way.
8:23
So let's assume
8:27
that we have a course type object
and we create a few of them.
8:28
Now let's assume
we create a new list of my courses.
8:32
Oh, whoops,
this top one here had a typo in it.
8:36
Now what
8:40
happens when I fix that typo in my course
this?
8:40
What happens to the course
that I put in the list?
8:44
Does it get the change? Aha!
8:46
Now I remember.
8:49
Those are just references
to the actual point in memory,
8:50
so of course that one has changed.
8:53
It's referencing the same object.
8:55
Alright, with all that information,
8:59
we're all set
to learn about our next interface. Set.
9:00
Right after this exercise.
9:05
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