Transcript for:
Arrays en Lijsten in Programmeren

  • Now, so far, we've only used variables that store one thing such as say float, that stores the number or a string that stores some texts. However often in programming, we want to work with an entire list of items. So in this video, we'll learn how to use lists and arrays to store many things in a single variable. But before we get into it, this video is sponsored by Core. Core is the new game creation platform that lets you build, publish, and play games. If you're just starting out making games, Core is a great solution as no coding skills are required. You can get started making games right away using their thousands of free high quality music sound and art assets. Core so that you create your own game logic in lure and build your own levels and art with the easy to use tools. From August 6th to September 6th, in collaboration with Dungeons & Dragons, Core's also hosting a fantasy themed dungeon building contest with $20,000 in prices. Content from this contest will also be showcased at PACS alongside a panel of celebrities who will be reliving some of their favourite moments from D&D campaigns, all recreated in Core. Again, Core is completely free so definitely go check it out at coregames.com or simply click the link in the description. Now, when we want to create a list of items such as my favourite characters in Game of Thrones, for example, the easiest way to do this in C sharp is with an array. Arrays allow us to store multiple values in a single variable. And the syntax for creating an array is actually fairly simple. First, we put the type of the variables that we want our array to store. In this case here, that's the string. Then we open and close two square brackets followed by the name of the variable. Now, if we already know what to put inside of our array, we can simply do that here. To do this we write an equal sign, then open and close curly brackets and put a semi colon. Inside the curly brackets, we put all the values we want separated by comas. And because we're using strings here, we need to remember the quotation marks. I'll put Varys, Cersei, Tyrion and Bronn. Definitely not Brenn, but Bronn. sorry, Brenn fans. Let the discussion commence. (laughing) And that's it, we've now created an array, a highly subjective array, but an array nonetheless. Now, when we we're creating an array, think of it like we're creating a table of items where each item has a number associated with it. We refer to this number as the index. The first item always has an index of zero. The second item, an index of one, then two and so on. And we use the index to access an element inside the array. If for example we want to print out my second favourite Game of Thrones character, we would write console.Write, and then input the name of the array followed by two square brackets. And this is where we specify an index. So because the index starts from zero to display second favourite Game of Thrones character, we input an index of one. And this indeed prints out Cersei. And this is cool in all but where arrays really start to shine is when we combine them with loops. So let's jump into VS code and try that out. So let's start out creating an array. I'm going to create a string array and I'm going to have it store some of favourite movies. And since I know these beforehand, we can go ahead and use the equal sign, then some curly brackets, followed by a semi colon. And in here we can put some of the movies. So I'm going to start with Lord of the Rings. I'm going to throw in Fight Club, Interstellar, and let's just put in Gladiator. Definitely a lot of good movies here. And we can actually loop through these movies using a for loop. So let's go ahead and create a for loop. I'm going to write for. I'm going to go down to here where it says for loop and hit Tab in order to auto completed it. I'm going to call my variable i here for index. And then the length here, I'm going to continue as long as i is less than, let's just put in 10. And that's it, we've now created a for loop. Now inside of this, we can go ahead and use console.WriteLine in order to write something out each time we go through the loop. And right now I'm just going to print out i. So we've created a for loop here that makes a variable code i and sets it equal to zero, it's then going to write out that number and keep doing this as long as i is less than 10. So if we just run our programme now we can see that this prints out the numbers zero through nine. And instead of just printing these numbers, we can use them as indexes to access the different elements in our array. So instead of just printing out console.WriteLine i, we can actually print out movies, and then open and close some square brackets, and inside of these, we can then put that i. So the first time we go through the loop, i is going to be zero, and so we're going to take the first element of our movies array which is Lord of the Rings. Then the second time i is going to be one, and so we're going to go to the second element of our array, which is Fight Club. And it's going to keep doing this as long as i is less than, and here we want to put in the length of our array. So in our case, we have four elements in here, so I'm just going to write four. And indeed, if we now run this programme, we can see that it no longer prints out the index, but instead actually accesses the element in the array with that index. So Lord of the Rings, Fight Club, Interstellar, and then Gladiator, awesome. And we could just write it like this, but instead of hard coding the length of our array like this, it's better practise to go movies.Length. This way if we later decide to add or remove an element from our movies array, this number is going to update and so for loop will always run the appropriate number of times. Now we could just show the movies like this, or we could add the ranking. So we can actually use our i variable to show what number the current movie is in our array. So before we print out the movie, we can create an integer, let's call it rank and set it equal to i+1. because I start at zero, we add the plus one here so that our rank will start at one, then go to two, then three and then four. What we can then do is add the rank variable to our write line. So here we can go and print out rank, plus, I'm going to add a dot and a space here, and then plus the movie at that index. So what we now get if we run this, are all of the same movies, but ranked one through four, awesome. So you can see how easy it is to work with arrays, as long as you remember this relationship between the index and the value that we're trying to access. But sometimes we don't know beforehand what we want in our array. If for example we want to use it to input their favourite movies, we can immediately assign values to the array. Instead when you do create an empty array first and then populate the values one by one. So let me remove the code here, and instead what we'll do is create an empty array. And to do this, we use the same syntax, so string, square brackets, let's call it movies. And then after the equal sign, we write new. Then the type of array in our case that's string, followed by the square brackets and then a semi colon. Then inside of the brackets, we get to specify the length. This is how many elements we want our array to store. In this case, let's just have the user input for movies so we'll set this to four, and there you go. We've now created an array that can hold four strings. Bring then to the user to input for movies, so we'll go console.WriteLine Type in four movies and then we can assign a value to each element in the array by using the index. So to assign the first movie, we go movies, then we give it the index of zero. And we set this equal to console.ReadLine, so whatever the user typed in. And we can do this four times, one for each index. So for the second one, we are going to put in one, for the third one, we are going to put in two, and for the fourth one, we'll put in three. And this should indeed work if we now run this programme, we can see that we are prompted to type in four movies. I'm going to type in Pulp Fiction, Inception, Harry Potter, and Karate Kid. And now we can't type in anymore, that will just close down the console. So as you can see, nothing is really happening here, but we are actually storing these values inside of our array. And we can now start to do things with them. If for example we wanted to display them in alphabetical order, we could use this sort method. So we'll add some extra lines here, we'll write out something to the user, so we'll go console.WriteLine and say something like here they are alphabetically. And I'm just going to add a new line at the start of that, just to make the formatting better. Then we can sort the array. To do that we go Array.Sort and here we simply input the name of our array. So in our case, that is movies. And that is actually going to go ahead and just sort it alphabetically. You can do this if you have a bunch of numbers as well, using the same function, which is going to sort them in ascending order. And then we can simply print out all the movies. Again, we could do this one by one, but it's much easier to use a for loop. So we'll write for in direct with zero. We'll keep going as long as i is this then movies.Length, and in here we'll just do console.WriteLine and then movies, input the i for the index and close that up with a semi colon. And before we test this, I just want to clean up our code a tiny bit, because just like we're using a for loop down here in order to print out all of our movies, let's also use a for loop up here when we're gathering input. There's really no reason why we should do this one by one. So instead of writing these four lines here, we can simply use a for loop, again, start the index at zero, keep going as long as i is less than movies.Length. And then we can take one of these lines here. And instead of putting in serial one, two or three, we just put in the i. That means that we can get rid of all of these lines right here, which is much, much cleaner, especially if you're working with a huge amount of items. And if we go ahead and run this programme now, we can type in four movies, hit Enter, and the computer sorts them alphabetically and displays them in that order, awesome. So that's just a simple example of how we can combine arrays with for loops in order to automate things when we're working with a lot of items. Of course, one of the disadvantages of using arrays is that they have a fixed size. This means that once we've created an array and given it a length, in this case, we've given it a length of four, we cannot change it. So if we don't know beforehand how many items we want to store, we instead use a list. When working with lists, we don't need to specify a length. They are totally resizeable. A good example of where this is useful is if we're creating a shopping list. Here we could use a list to keep track of all the items we need to purchase. Now to use a list, we actually need to include another namespace. You can think of namespaces like groups of functionality that we can import in order to do different things. For example, when making a game, we often have a namespace dedicated to audio, one for graphics, one for input and so on. So far, we've just been using default system namespace. We can see that here at the top. This includes the console class that we've been using so much. But lists are inside another namespace. So at the top, we need to add another line and he will write using followed by the namespace we want to include. Lists are inside of this System.Collections.Genericnamespace. So just by writing that here, we've now included all of this functionality that we can now use in our programme. And with that, we can start using lists. Now a list is something that we call a generic, which makes the syntax a bit different from a normal array. To create a list, we first type list, then a list, then sign followed by the type in our case we want this to be string and then a greater than sign. We then put a name here. I'm going to type shopping list. And just like when we created an empty array, we also need to create an empty list. Again the syntax for this is a bit weird, but just type after me. So create an equal sign here. We'll then type new list of type string followed by two parentheses and a semi colon. And that it, we've now created a list. And this is by far the most scary part of working with lists. From here on, it's actually really simple. If we want to add an item to our list, we simply go shoppingList.Add and then we put in what we want to add. Now I'm going to go shopping in a pretty unconventional place, The Intergalactic Heaven Express Mall. And here they sell items such as dreams. So I'm going to add that to my shopping cart. Most are going to add another thing here, so I'll add another line. I'm going to add miracles and let's just do a couple more, rainbows and maybe a pony. So now that we've added all these things to our list, we can go ahead and print them out. And this works in the exact same way as a normal array. We can use a for loop, that's a Tab again to auto complete, and we're going to loop through as long as i is this then shoppingList, dot and when working with lists, we cannot write length here. Instead, we have to write count. Don't ask me why, but that's just how it is. So just remember that when working with arrays to get the length, you write length, and when working with lists, you write count, simple to that. And that's pretty much the only thing that's different. We can now go console.WriteLine, access the shopping list, type some square brackets and put in i as the index. And that's it. If you now hit a five and run this, we can see it prints out all the items in our shopping list. So far so good, but we could just have done this with a normal array. One of the things that makes lists really handy is that we can easily remove from them. So if we decide to scratch a couple of things from our shopping list, we can simply go shoppingList.Remove and then input the item we want to remove. Say, I figured out that it already had some dreams left over in the fridge, I could simply remove that from the list. And we can actually also choose to remove using an index. So we can go shoppingList.RemoveAt, and then input some kind of index here. So if we want to remove the second item we can put in one. And with these things removed, let's display the list once more. So I'm just going to go ahead and do console.WriteLine. I'm just going to write a bunch of dashes here just to kind of separate the two lists, and then we can do another for loop. In fact, we can simply copy the one from up here, down here. And if we now run this, I'm just going to place this over here. As we can see it prints out the entire list with dreams, miracles, rainbows, and a pony. It then removes the dreams and then removes the second item. But because we've already removed dreams, our first item is now miracles with an index of zero. And the second item is rainbows with an index of one. And so it went ahead and removed the rainbows, which leaves us with the miracles and a pony, still a pretty exciting list. So that's how you can get started using lists and arrays to store multiple items. Now, for this week's challenge, I want you to create a programme that allows a teacher to input all the students in the class and have their names printed out alphabetically. You can decide for yourself if you want to use an array or a list. In my example, I've used an array. Let me just give you a quick demo. As you can see, the programme starts by asking how many students are in the class? I'm just going to put in four here. And the reason why we're doing this is so that I know how large of an array I should create. Here we need to store four students. So I'm creating an array of that size. Then it says, please input the names of the students. I'm going to put in John Wick, Harry Potter, Dwayne Johnson and Shrek. And as you can see, it creates this line here and then prints out the exact same list, but in alphabetical order, awesome. So have fun with it. And always I've posted my solution to this week's challenge on the Brackeys forum. That's of course, link for that in the description. That's pretty much it for this video. If you enjoyed it, make sure to subscribe and ring that notification bell so don't miss the next one. Also, don't forget to check out Core. It's completely free. Just visit coregames.com or click the link in the description. On that, thanks for watching. And I will see you in the next video. Thanks to all of the awesome patreon supporters who donated in July, and a special thanks to Dante Sam, Lost to Violence, Loved Forever, Niftylius, Scott McKee, faisal marifie, Replica Studios, Leo Lesetre, Nubby Ninja, Jason Uritescu, Piano Sathornlak, bobby reynolds, Donatien Gascoin. Mark-Antoine Girard,Jacob Sanford, Michail Korobov,Naoki Iwasaki,Gregory Pierce, Owen Cooper, The Mighty Zeus, Erasmus of EekGames, I Love Brackeys, SiriusWolf, Fred Mastro, Hassan Sher,Storm Daniels, and Dennis Solomon. You guys rock.