Arrays4:14 with Jeremy McLain
Arrays are a numbered list of items.
The simplest type of a collection is an array. 0:00 In fact most other complex collection types 0:03 use arrays under the hood in some form or another. 0:06 We can think of an array as a numbered list of items. 0:10 We refer to this number as the items index in the array. 0:13 This allows us to identify each item based on its location. 0:17 For example, we can say we want the third item in the array. 0:22 As in most programming languages we start counting zero. 0:26 So the first item in the array has an index of zero. 0:30 The index of the second item is one and so on. 0:34 The end of the last item is one less than the length of the array. 0:37 Let's refresh our memories on how to work with arrays and 0:42 then learn about their advantages and their disadvantages. 0:45 To follow along, open work spaces and in the consul type csharp to start the repel. 0:48 To instantiate an array, 0:55 we first declare the type of item we want to store in the array. 0:57 And then add opening and closing square brackets. 1:01 Here I'll give the array a name. 1:04 I'll say lockCombination. 1:06 Now we set it equal to a new integer array. 1:10 And inside this square brackets we need to tell how large we want the array to be. 1:15 So I'll say, three. 1:19 One disadvantage of using an array is we have to tell it the maximum number of 1:22 items we expect to put in the array. 1:27 We'll see in a bit that changing the size of an array later is not trivial. 1:29 Notice that all of the items in the array are initialized to the type's 1:33 default value. 1:37 In the case of numeric types like integer, it's zero. 1:39 In the case of other types, all of the items will be null. 1:43 Items in a collection such as an array are also known as its elements. 1:46 You'll hear me use the terms elements and items interchangeably. 1:51 Another way we can instantiate an array 1:54 is by initializing it at the time we create it. 1:57 So here in between curly braces. 2:00 I can say 10, 5 and 32, for my lock combination. 2:03 When we create an array like this, 2:11 we don't need to put the size of the array here between the square brackets. 2:13 The length of the array is just the number of items in the initialization list here. 2:18 We also don't need to specify the type of the array when we do this. 2:25 The type of the array can be inferred from the types of the items, so 2:29 long as they're all the same type. 2:33 In fact, we don't even need to put this new with the square braces here. 2:35 The advantage of an array is, 2:40 in how quickly we can retrieve an item when we already know its index. 2:42 So I'll get the last item in this lock combination array and 2:46 I'll assign it to a variable called last. 2:49 So I'll say, int last = lockCombination and 2:51 then to tell it which item in the array I want. 2:59 I'll just put it's index here. 3:01 Now the variable last contains 32. 3:04 The reason retrieving an item from an array is so fast, 3:08 is because items in an array are stored sequentially in the computer's memory. 3:12 Knowing an item's index is the same as knowing its physical address and memory. 3:16 So, when we tell the computer we want the fourth item in the array, 3:21 we can find it right away without having to count from the beginning of the array 3:25 to the fourth item, bam! 3:29 When it comes to accessing items, nothing beats an array. 3:31 The same goes for updating items in an array. 3:35 We're just overwriting the item at the index specified. 3:38 So if I wanted to change the last item in the lock combination array. 3:42 I just say lockCombination[2} = 20. 3:46 Setting or updating an item in an array when we know the index we want to set, 3:52 is just as fast as setting a variable. 3:58 Arrays are also good when we want to keep our items in order. 4:01 This makes an array a good choice for 4:06 storing things like combinations to a lock. 4:08 Once we put an item at an index it stays there. 4:11
You need to sign up for Treehouse in order to download course files.Sign up