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
Let's take a closer look at how to use the List class.
-
0:00
So far we've seen how to create a list, add items to it and
-
0:04
access those items using their index.
-
0:06
We can add items to the list at the time it's created by using a collection
-
0:10
initializer.
-
0:11
We simply instantiate our list exactly as we had before.
-
0:18
Before typing the semi-colon we add opening and closing curly braces here.
-
0:25
We still need the ending semicolon though.
-
0:28
Now we can list the items we want our list to start out with just like we did with
-
0:32
arrays.
-
0:32
So I'll say Sue.
-
0:36
Bill.
-
0:41
Allen.
-
0:45
Beth.
-
0:48
And Mary.
-
0:52
Notice that even though we've only listed five items here,
-
0:55
the capacity of the list is eight.
-
0:57
This is because the collection initializer actually just calls the list add method.
-
1:03
This is exactly like creating the list and then calling the add method five times.
-
1:08
The list had two resize itself to a capacity of eight
-
1:12
when it got to the fifth item.
-
1:14
Again this is a good time to give the list an initial capacity and
-
1:17
avoid some overhead.
-
1:18
[BLANK AUDIO] So we can say five right here.
-
1:26
Another way to instantiate a list is with another collection
-
1:30
by passing it to the constructor.
-
1:32
So let's create another list called students2 and
-
1:37
we'll say new list string.
-
1:42
And we'll pass in the students list we created before.
-
1:48
Passing in another list essentially makes a copy of the list.
-
1:55
I could have passed in any collection type here, in fact,
-
1:59
passing in an array here is a good way to convert an array into a list.
-
2:05
We can convert a list to an array by calling two array.
-
2:09
So let's create a string array here.
-
2:12
Call it studentArray.
-
2:17
And set equal to students.Toarray.
-
2:27
We can iterate through all of the items in a list the same way we did with
-
2:31
the arrays.
-
2:31
We will use a foreach loop but we could use a for
-
2:37
loop for a while loop so say foreach string student in students.
-
2:48
And then we'll just print them to the console.
-
2:50
So I'll say console.writeline student.
-
3:00
We can also insert items anywhere in the list using the Insert method.
-
3:06
So I'll say students.Insert.
-
3:10
And here we specify the index that we want to insert the new item at so
-
3:15
I'll put something right it index 1.
-
3:19
And will insert Frank.
-
3:25
When we enter items into a list all of the items after where we inserted
-
3:30
the item get moved up.
-
3:32
So Bill, Allen, Beth, and Mary all had to be moved up.
-
3:37
This is not a quick task if there are a lot of items.
-
3:40
Also if there isn't enough room to add the item, then the list is resized.
-
3:45
There are a couple of ways to remove items from a list.
-
3:49
If we know the index of the item that we want to remove,
-
3:51
then we can use the remove at method.
-
3:53
Let's say Bill moved to another school so we need to remove him from the list.
-
3:58
If we happen to already know that Bill is at index 2,
-
4:05
we can say students.removeAt 2.
-
4:10
Now if we take a look at students, we can see that Bill is gone and
-
4:15
all of the items after where Bill was have been shifted back down in the list.
-
4:20
Like arrays, lists are not optimized to have items inserted into or
-
4:24
removed from the middle of them.
-
4:26
However, this usually isn't a big problem.
-
4:29
You'll find that inserting and deleting items from the middle of a list
-
4:33
is actually not as common in everyday programs as you might think.
-
4:36
There are collections that are designed to make adding and
-
4:39
removing items much faster though.
-
4:41
I'll mention some of these more specialized collections near the end of
-
4:44
this course.
You need to sign up for Treehouse in order to download course files.
Sign up