‫Welcome back.
‫In this video, we're going to check out lists which are a type of collection.
‫And it's a flexible way to work with sets of objects or values other than an array, for example.
‫So compared to your race to group of objects or values you work with can grow and shrink dynamically.
‫And as you might remember in a race, you had to set that specifically.
‫So it was set in stone.
‫How many elements an array would have?
‫But lists are quite dynamic here.
‫A collection is a class and it requires an instance of the class before adding elements to that collection.
‫So when we are working with lists, lists are collections.
‫So lists is a type of a collection, so it's inheriting from it, but it's still a class.
‫So we need to add the following line.
‫So for collections that only contain one data type, use this system dot collections, dot generic namespace.
‫So we will need to add this namespace to our project if you want to use lists list declaration examples
‫here.
‫So var numbers is equal new list int.
‫So here we're talking about a list without any values.
‫So it's an empty list at that point.
‫And then below that you see an example where the list does have values.
‫So the difference is here you add the values in curly brackets here, separated by a comma, then adding
‫and removing values.
‫So in order to add a value, you can simply use the add method.
‫In order to get rid of a value, you use the remove method.
‫So here we see we add the seven to our list and then we remove the seven from our list.
‫Then we have an example of index.
‫So index is zero and remove add index will remove an element at a specific position.
‫So it's similar to an array where you have indexes and you can simply remove an element at a specific
‫index getting a value.
‫So here you see we have this numbers list which has one value which is 25.
‫And now in order to get that value, we simply use the index.
‫And in this case, because we only have one value, the index for it is zero.
‫So numbers zero will be 25 and that will now be stored in a value.
‫In order to clear a list, you can simply use the clear method.
‫So there is a specific method dedicated to clearing the whole list so it will be empty afterwards.
‫So it doesn't matter how many elements you have added, it will simply get rid of all of them.
‫Of course, only specific to that particular list that we're talking about, lists and loops.
‫So here you see an example of a list with plenty of numbers.
‫So 5 to 40 each time, a five increment.
‫And we use a for each loop here in order to loop through every single element in that list or a for
‫loop.
‫So you have both options here.
‫The for each loop is a little bit cleaner as you can see here for each int element in numbers.
‫And then we simply print out the element.
‫And the same thing here for this for loop where we just check the amount of numbers of course as well.
‫So you can see there is a property called Count that is available for every single list that you have.
‫So in this case, we're just checking the amount of values that we have in that list in order to not
‫run into an index out of bounds exception.
‫All right.
‫So this was an introduction to lists, and we're going to use lists later on.
‫And I just wanted to show you how they work and what the main aspects of them are.
‫Of course, lists have multiple more methods.
‫So you can, of course, check those out, check out the methods and properties of lists and play around
‫with them to get a better feeling for them and just create a handful of lists just to get into the habit
‫of practicing.
‫All right.
‫So let's go ahead in the next video.
‫See you there.