1 00:00:00,510 --> 00:00:03,580 The simplest type of a collection is an array. 2 00:00:03,580 --> 00:00:06,440 In fact most other complex collection types 3 00:00:06,440 --> 00:00:08,930 use arrays under the hood in some form or another. 4 00:00:10,310 --> 00:00:13,730 We can think of an array as a numbered list of items. 5 00:00:13,730 --> 00:00:17,570 We refer to this number as the items index in the array. 6 00:00:17,570 --> 00:00:22,020 This allows us to identify each item based on its location. 7 00:00:22,020 --> 00:00:25,610 For example, we can say we want the third item in the array. 8 00:00:26,650 --> 00:00:30,130 As in most programming languages we start counting zero. 9 00:00:30,130 --> 00:00:34,200 So the first item in the array has an index of zero. 10 00:00:34,200 --> 00:00:37,510 The index of the second item is one and so on. 11 00:00:37,510 --> 00:00:42,030 The end of the last item is one less than the length of the array. 12 00:00:42,030 --> 00:00:45,020 Let's refresh our memories on how to work with arrays and 13 00:00:45,020 --> 00:00:47,780 then learn about their advantages and their disadvantages. 14 00:00:48,820 --> 00:00:55,700 To follow along, open work spaces and in the consul type csharp to start the repel. 15 00:00:55,700 --> 00:00:57,490 To instantiate an array, 16 00:00:57,490 --> 00:01:01,190 we first declare the type of item we want to store in the array. 17 00:01:01,190 --> 00:01:04,940 And then add opening and closing square brackets. 18 00:01:04,940 --> 00:01:06,710 Here I'll give the array a name. 19 00:01:06,710 --> 00:01:09,010 I'll say lockCombination. 20 00:01:10,260 --> 00:01:14,010 Now we set it equal to a new integer array. 21 00:01:15,430 --> 00:01:19,650 And inside this square brackets we need to tell how large we want the array to be. 22 00:01:19,650 --> 00:01:20,800 So I'll say, three. 23 00:01:22,530 --> 00:01:27,030 One disadvantage of using an array is we have to tell it the maximum number of 24 00:01:27,030 --> 00:01:29,110 items we expect to put in the array. 25 00:01:29,110 --> 00:01:33,500 We'll see in a bit that changing the size of an array later is not trivial. 26 00:01:33,500 --> 00:01:37,160 Notice that all of the items in the array are initialized to the type's 27 00:01:37,160 --> 00:01:37,910 default value. 28 00:01:39,450 --> 00:01:43,240 In the case of numeric types like integer, it's zero. 29 00:01:43,240 --> 00:01:46,600 In the case of other types, all of the items will be null. 30 00:01:46,600 --> 00:01:51,060 Items in a collection such as an array are also known as its elements. 31 00:01:51,060 --> 00:01:54,910 You'll hear me use the terms elements and items interchangeably. 32 00:01:54,910 --> 00:01:57,400 Another way we can instantiate an array 33 00:01:57,400 --> 00:02:00,690 is by initializing it at the time we create it. 34 00:02:00,690 --> 00:02:03,940 So here in between curly braces. 35 00:02:03,940 --> 00:02:11,780 I can say 10, 5 and 32, for my lock combination. 36 00:02:11,780 --> 00:02:13,680 When we create an array like this, 37 00:02:13,680 --> 00:02:17,040 we don't need to put the size of the array here between the square brackets. 38 00:02:18,310 --> 00:02:22,660 The length of the array is just the number of items in the initialization list here. 39 00:02:25,300 --> 00:02:28,700 We also don't need to specify the type of the array when we do this. 40 00:02:29,860 --> 00:02:33,580 The type of the array can be inferred from the types of the items, so 41 00:02:33,580 --> 00:02:34,950 long as they're all the same type. 42 00:02:35,970 --> 00:02:40,230 In fact, we don't even need to put this new with the square braces here. 43 00:02:40,230 --> 00:02:42,060 The advantage of an array is, 44 00:02:42,060 --> 00:02:46,080 in how quickly we can retrieve an item when we already know its index. 45 00:02:46,080 --> 00:02:49,820 So I'll get the last item in this lock combination array and 46 00:02:49,820 --> 00:02:51,580 I'll assign it to a variable called last. 47 00:02:51,580 --> 00:02:59,260 So I'll say, int last = lockCombination and 48 00:02:59,260 --> 00:03:01,915 then to tell it which item in the array I want. 49 00:03:01,915 --> 00:03:03,170 I'll just put it's index here. 50 00:03:04,980 --> 00:03:08,420 Now the variable last contains 32. 51 00:03:08,420 --> 00:03:12,000 The reason retrieving an item from an array is so fast, 52 00:03:12,000 --> 00:03:16,790 is because items in an array are stored sequentially in the computer's memory. 53 00:03:16,790 --> 00:03:21,490 Knowing an item's index is the same as knowing its physical address and memory. 54 00:03:21,490 --> 00:03:25,740 So, when we tell the computer we want the fourth item in the array, 55 00:03:25,740 --> 00:03:29,220 we can find it right away without having to count from the beginning of the array 56 00:03:29,220 --> 00:03:31,600 to the fourth item, bam! 57 00:03:31,600 --> 00:03:35,250 When it comes to accessing items, nothing beats an array. 58 00:03:35,250 --> 00:03:38,690 The same goes for updating items in an array. 59 00:03:38,690 --> 00:03:42,430 We're just overwriting the item at the index specified. 60 00:03:42,430 --> 00:03:46,550 So if I wanted to change the last item in the lock combination array. 61 00:03:46,550 --> 00:03:52,630 I just say lockCombination[2} = 20. 62 00:03:52,630 --> 00:03:58,490 Setting or updating an item in an array when we know the index we want to set, 63 00:03:58,490 --> 00:04:01,240 is just as fast as setting a variable. 64 00:04:01,240 --> 00:04:04,220 Arrays are also good when we want to keep our items in order. 65 00:04:06,310 --> 00:04:08,380 This makes an array a good choice for 66 00:04:08,380 --> 00:04:11,350 storing things like combinations to a lock. 67 00:04:11,350 --> 00:04:14,040 Once we put an item at an index it stays there.