Check out a free preview of the full Java Fundamentals course

The "List" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates how to define a collection of ordered elements accessible by position, also known as a List. Replacing items in an array list using .set and removing an item using .remove are also covered in this segment.

Preview
Close

Transcript from the "List" Lesson

[00:00:00]
>> So list is a collection where the elements are ordered. They are ordered, okay? And the list can contain duplicate elements. So an example of where lists might be used would be, let's say, the phone numbers from your call history. If you looked at your call history, there in order as the calls came in.

[00:00:22]
And you can have duplicates if mom called twice, for example. So we're gonna go ahead and create a list of fruit. And we'll do the same sort of exercises we did with list, all right? So back to our collections demo class. I am going to collapse the setDemo.

[00:00:43]
We'll comment this out. And let's do a list demo now. We need to create that public static void_listDemo. All right, so to create a list we can use this as the data type list as the data type. We'll call this fruits. You need to import list. It also comes from java.util and a list is an interface, which means we cannot instantiate it.

[00:01:24]
One of its implementing classes is ArrayList. We'll need to import that as well. Wait a minute. Do I do the right list? No, this is the wrong list. [LAUGH] I imported the wrong list. Okay, there we go. All right, to add, we use the same add method. So let's add an apple, let's add a lemon.

[00:02:12]
We can add banana. Let's add an orange. Okay, and let's print the list. So if I run this, it should stay in order. Yes, so I added the apple, the lemon, the banana, and the orange. And that's exactly the order that they appear, so that's great. You can also replace an item in a list.

[00:02:59]
So let's say, for example, I want to replace this third item in the list with a grape so I want to replace the banana with a grape, right? Because the objects are in a specific order, you can access these by index. Okay, so if I wanna replace, I can say fruits.set, set is what it is.

[00:03:32]
So set takes an index, what's the index of the item that you want to remove? And then give me the new object that you want to put there in its place. So indexes start with zero. If I wanna remove the banana, was the index for banana?
>> Two.

[00:03:52]
>> Two, and then I can give it the new object that I wanna put there. So that should remove the banana and put a great there in its place. Let's make sure that happens. Yes, I got an apple and grape and then an orange, right? Again, list do allow duplicate elements, so I can add another lemon here for example and it will actually appear.

[00:04:26]
So we have two lemons now. If I want to remove an element, I can say remove, and I can, this is an overloaded method. There's two of them that I can use, I can say remove, and I can give it an object name. I mean the object that I want removed or I can remove it by its index.

[00:04:52]
So if I wanted to remove lemon, let's see what would happen if I put lemon. Do you think they would remove both of the lemons? Or the first lemon that it found, or the last lemon that it found, what would happen? Let's run it and see. So it would remove the first instance of what it found, but what if that wasn't the one you wanna remove?

[00:05:18]
But if it was the last one you wanna remove, then you would use the index, so that you could specify exactly which one you want it gone. Removes first found. And then let's see. Let's just give the other example, so you have this one. I wanna remove element what's the lemon here after I remove this one zero one, two, three?

[00:05:50]
I wanna remove three, so that should take care of both lemons.
>> I have a question, if you don't specify if say you did there's three lemons and you specified a fruit that was above lemon that wasn't lemon. Would it just once it hits that lemon, it just goes down the list.

[00:06:09]
>> I'm sorry, say it again.
>> If you did the indexes, one it's apple, and it's not lemon. Would it just go down on the list until it finds lemon or would it just throw you an error?
>> Tell me which line you're talking about 44?
>> 36, if you specified that index like apple, but you're looking to remove lemon.

[00:06:30]
And obviously apples and the lemon. What would happen? Would it throw you an error or would it go down?
>> No, it doesn't know. It wouldn't know, right? So if you said, I added an apple and then I changed this to say remove index zero, which is supposed to be apple, it has no idea that you really meant lemon.

[00:06:53]
>> Okay.
>> Yeah, it's just gonna do it. And there's no adding back either so. [LAUGH] I mean, you could say die add, but these are very simple objects that are strings. If you had like a bigger object that has stayed in all of that stuff, an account, for example, you just kind of blew somebody's account.

[00:07:15]
>> I meant for the removes first found, that lemon. Never mind, I think I get it.
>> [LAUGH]
>> Removes first lemon.
>> Yeah, just, it-
>> Yeah, go down until it finds a lemon and then remove the first one.
>> Yeah.
>> Yeah, all right, if you want to again, these things are ordered.

[00:07:41]
So if you wanted to do something like print out what is in a specific spot, you can do that as well. So let's say you wanna know what is at index two. I can simply say fruits.get. So at get, I will specify the index and then it will give me the object that is at that index.

[00:08:18]
So let's print that out. Index two is orange, okay? If I need to find an index, I don't know where it is in the list, right? I know that this object is there. I can say fruits.index of, and I can specify an object, so let's say grape and it'll give me the index for grape.

[00:08:57]
So let's say index of grape. And then I said fruits.index of I gave it the object and then it'll tell me okay grape is at index one. Also, if I want, let me not remove these lemons. I can do something like the last index of. So fruits.LastIndexof and then I specify lemon.

[00:09:45]
We print this and it says okay the last index of lemon is 4. Now question? Go ahead.
>> If these array lists are so much more powerful than sets, why would you ever want to use a set
>> Why do you think they're more powerful?
>> Well, there's more flexibility because you control both the index and the position and order and you can do more things with them, even as an unordered group.

[00:10:24]
>> So what if I don't want to do those things? That's not always powerful, right? What if I want a collection that does not have any duplicates? I don't want duplicates in here, like a deck of playing cards, for example. So list is not helpful for me there.

[00:10:41]
So if I need those restrictions, that's when you will use that, okay? You can also create the list with the list data. So that immutable one, so more fruit equals lists that have and I'll add like maybe two fruit here, cherry and a plum. Again, this creates an immutable list.

[00:11:12]
So I would not be able to do any adding, removing, anything like that. I could do some of these operations with the index, does it contain this? I can do that sort of thing. I cannot do that set where I'm replacing an object I cannot add, I cannot remove, okay, so immutable.

[00:11:36]
Questions on list.
>> Can you repeat again what last index does?
>> Yeah, so last index of is when you have since this collection contains duplicate or occurred, contain duplicate. Maybe you only want the last index. If I do index of lemon, it's going to give me the index of the first one.

[00:11:59]
Lemon appears twice in our list. So last index will say, find me the last index of that item.
>> Is there a first index of?
>> Let's see index.
>> No, so index of is the first one. Now let's say you have five of these things. I don't know how you would get to like what's in the middle, right?

[00:12:24]
>> Can you do index of and have a start?
>> That's kind of what I was trying to do.
>> Let's see if there's an overloaded one of this. Nope, you got two. Index of and last index of, yeah.
>> So yeah, that's kind of like what I was wondering before.

[00:12:39]
Like with the first one, you can tell where to start to look on the list, and would it just go down the list, but apparently not.
>> Right, and then there was one, like get, right? So get gives us. So the object itself, right? I'm thinking how I would go about doing that.

[00:13:08]
So if I wanted the indexes of every limit in a list, I would probably loop through that list. And then like kind of store them somewhere.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now