1 00:00:00,720 --> 00:00:01,560 ‫Welcome back. 2 00:00:01,590 --> 00:00:06,210 ‫In this video, we're going to check out lists which are a type of collection. 3 00:00:06,210 --> 00:00:13,020 ‫And it's a flexible way to work with sets of objects or values other than an array, for example. 4 00:00:13,020 --> 00:00:19,170 ‫So compared to your race to group of objects or values you work with can grow and shrink dynamically. 5 00:00:19,170 --> 00:00:24,300 ‫And as you might remember in a race, you had to set that specifically. 6 00:00:24,300 --> 00:00:25,950 ‫So it was set in stone. 7 00:00:25,950 --> 00:00:28,200 ‫How many elements an array would have? 8 00:00:28,200 --> 00:00:31,050 ‫But lists are quite dynamic here. 9 00:00:31,500 --> 00:00:38,280 ‫A collection is a class and it requires an instance of the class before adding elements to that collection. 10 00:00:38,280 --> 00:00:42,150 ‫So when we are working with lists, lists are collections. 11 00:00:42,150 --> 00:00:49,410 ‫So lists is a type of a collection, so it's inheriting from it, but it's still a class. 12 00:00:49,410 --> 00:00:52,140 ‫So we need to add the following line. 13 00:00:52,140 --> 00:00:58,080 ‫So for collections that only contain one data type, use this system dot collections, dot generic namespace. 14 00:00:58,080 --> 00:01:05,730 ‫So we will need to add this namespace to our project if you want to use lists list declaration examples 15 00:01:05,730 --> 00:01:06,090 ‫here. 16 00:01:06,090 --> 00:01:10,620 ‫So var numbers is equal new list int. 17 00:01:10,620 --> 00:01:13,920 ‫So here we're talking about a list without any values. 18 00:01:13,920 --> 00:01:16,350 ‫So it's an empty list at that point. 19 00:01:16,350 --> 00:01:21,240 ‫And then below that you see an example where the list does have values. 20 00:01:21,240 --> 00:01:30,900 ‫So the difference is here you add the values in curly brackets here, separated by a comma, then adding 21 00:01:30,900 --> 00:01:31,950 ‫and removing values. 22 00:01:31,950 --> 00:01:35,460 ‫So in order to add a value, you can simply use the add method. 23 00:01:35,460 --> 00:01:39,900 ‫In order to get rid of a value, you use the remove method. 24 00:01:39,900 --> 00:01:47,670 ‫So here we see we add the seven to our list and then we remove the seven from our list. 25 00:01:47,850 --> 00:01:50,700 ‫Then we have an example of index. 26 00:01:50,700 --> 00:01:57,900 ‫So index is zero and remove add index will remove an element at a specific position. 27 00:01:57,900 --> 00:02:04,980 ‫So it's similar to an array where you have indexes and you can simply remove an element at a specific 28 00:02:04,980 --> 00:02:07,380 ‫index getting a value. 29 00:02:07,380 --> 00:02:12,510 ‫So here you see we have this numbers list which has one value which is 25. 30 00:02:12,510 --> 00:02:16,140 ‫And now in order to get that value, we simply use the index. 31 00:02:16,140 --> 00:02:19,980 ‫And in this case, because we only have one value, the index for it is zero. 32 00:02:19,980 --> 00:02:26,100 ‫So numbers zero will be 25 and that will now be stored in a value. 33 00:02:26,490 --> 00:02:30,570 ‫In order to clear a list, you can simply use the clear method. 34 00:02:30,570 --> 00:02:36,450 ‫So there is a specific method dedicated to clearing the whole list so it will be empty afterwards. 35 00:02:36,450 --> 00:02:41,220 ‫So it doesn't matter how many elements you have added, it will simply get rid of all of them. 36 00:02:41,310 --> 00:02:46,530 ‫Of course, only specific to that particular list that we're talking about, lists and loops. 37 00:02:46,530 --> 00:02:49,690 ‫So here you see an example of a list with plenty of numbers. 38 00:02:49,690 --> 00:02:53,460 ‫So 5 to 40 each time, a five increment. 39 00:02:53,460 --> 00:03:00,870 ‫And we use a for each loop here in order to loop through every single element in that list or a for 40 00:03:00,870 --> 00:03:01,140 ‫loop. 41 00:03:01,140 --> 00:03:03,090 ‫So you have both options here. 42 00:03:03,120 --> 00:03:09,810 ‫The for each loop is a little bit cleaner as you can see here for each int element in numbers. 43 00:03:09,810 --> 00:03:12,030 ‫And then we simply print out the element. 44 00:03:12,030 --> 00:03:17,760 ‫And the same thing here for this for loop where we just check the amount of numbers of course as well. 45 00:03:17,760 --> 00:03:24,420 ‫So you can see there is a property called Count that is available for every single list that you have. 46 00:03:24,420 --> 00:03:31,680 ‫So in this case, we're just checking the amount of values that we have in that list in order to not 47 00:03:31,680 --> 00:03:34,590 ‫run into an index out of bounds exception. 48 00:03:34,590 --> 00:03:35,130 ‫All right. 49 00:03:35,130 --> 00:03:39,300 ‫So this was an introduction to lists, and we're going to use lists later on. 50 00:03:39,300 --> 00:03:45,210 ‫And I just wanted to show you how they work and what the main aspects of them are. 51 00:03:45,240 --> 00:03:48,300 ‫Of course, lists have multiple more methods. 52 00:03:48,300 --> 00:03:52,440 ‫So you can, of course, check those out, check out the methods and properties of lists and play around 53 00:03:52,440 --> 00:03:58,770 ‫with them to get a better feeling for them and just create a handful of lists just to get into the habit 54 00:03:58,770 --> 00:04:00,360 ‫of practicing. 55 00:04:00,870 --> 00:04:01,260 ‫All right. 56 00:04:01,260 --> 00:04:03,450 ‫So let's go ahead in the next video. 57 00:04:03,450 --> 00:04:04,170 ‫See you there.