Arrays5:22 with Jeremy McLain
Collections of objects can be stored in an array.
[MUSIC] 0:00 Up until now we've been dealing with individual objects. 0:04 Tree house Defense will need to be able to deal with groups of objects. 0:08 For example, we'll have many towers and many invaders. 0:13 It'd be pretty difficult to keep track of all of these objects if we had to create 0:16 a separate variable for each tower and give each variable a different name. 0:20 What we need is to be able to keep track of them as a collection of objects. 0:25 There's a lot of ways to represent a collection of objects. 0:31 The simplest is an array. 0:34 You can think of an array as a numbered list of items, 0:37 we refer to this number as an items Index in the array. 0:40 This allows us to identify each item based on its location in the list. 0:44 For example, we can say we want the third item in the list. 0:49 Remember how we learned earlier if we start counting at zero in programming. 0:54 Well, we do that with arrays and other collections, too. 0:58 So the third item in the list actually has an index of 2. 1:02 Let's learn how to work with arrays in C Sharp. 1:06 To learn about arrays, let's open the C Sharp Rebel. 1:10 To open the C Sharp Rebel in workspaces, just type C sharp in the consul. 1:13 To demonstrate how to create an array let's create an array of strings. 1:18 To do this, we first specify that the type of objects we want to store in the array. 1:22 In our case, it's string. 1:27 To make this an array of strings we had opening and 1:29 closing square brackets here after the typing. 1:32 Then we give the array of name. 1:35 I'll call it favoriteThings. 1:37 The square brackets here signifies that this is an array. 1:39 We just declared a string or a variable, but we haven't put anything inside it yet. 1:43 In fact it isn't even an array yet 1:47 to see why type the variable name favoriteThings in the C Sharp repo. 1:50 It printed out null. 1:58 Null has a special meaning in C sharp. 2:00 It represents the absence of a value. 2:02 For most types, when we declare a variable without assigning anything, 2:05 it's set to null by default. 2:08 The exception to this rule is numeric types and struct types. 2:10 Numeric types such as int and double have a default value of zero. 2:15 We'll learn about struct types in future courses. 2:20 A string array is just another type of class. 2:23 In order to create one we need to instantiate it. 2:26 We do this similar to the way we instantiate any other type, by using new. 2:29 Only, we don't need to provide parentheses at the end. 2:34 We do need to specify how long the array should be. 2:38 We put how many items arrays can hold right here between the square brackets. 2:41 I'll make it three items long. 2:46 We have an array that can hold three strings. 2:48 To see what we did, type favorite things in the console again. 2:50 What we're seeing is the contents of the array. 2:55 As you can see, everything is null. 2:58 But now there are three nulls, one for each item in the array. 3:00 Their null because we haven't said what we want our three things to be it. 3:04 There are a couple ways we can do that. 3:09 First, we can set individual items. 3:12 Let's set the first item in the array to sunshine. 3:14 We do that by first saying which index in the array we want to change and 3:18 then assign it a value. 3:21 Notice that I type the index, 0, right here in between the square brackets. 3:28 Let's take a look at the array now. 3:33 Now the first item in the array has been changed to sunshine. 3:37 We can get the value from a specific location in the array like so. 3:41 By using index zero, we're getting the first item in the array. 3:48 Sometimes we want to set all of the items in the array at the same time that we 3:52 declare it. 3:56 We can do that by using curly braces at the end here, with commas between them. 3:57 So here where we're declaring the array, I'll type sunshine, 4:02 presents, and babies. 4:10 Let's see what our array looks like now. 4:17 Now we have three items in the array and none of them are null. 4:19 There's an easier way to do this because we're providing the list of items 4:23 here when the arrays created. 4:27 The compiler already knows how many items we want in it. 4:29 So we don't need to type three again here. 4:33 In fact, we can make this even shorter. 4:36 All three of our items are strings, and the compiler knows this. 4:39 So we don't need to specify the type here. 4:43 Arrays are so common that Csharp has an even shorter syntax for initializing them. 4:47 We can delete the new keyword and the square brackets and 4:53 just list the items inside the curly braces like so. 4:56 Finally, we often want to know how long an array is. 5:00 For that, we can type favoriteThings.Length. 5:04 This will always give us the full length of the array 5:12 that includes any null items too. 5:16 Now that we know a bit about arrays let's put them to work for us. 5:18
You need to sign up for Treehouse in order to download course files.Sign up