1 00:00:00,850 --> 00:00:04,290 Earlier in this course, we learned what arrays are good at. 2 00:00:04,290 --> 00:00:06,640 They're blazingly fast at retrieving or 3 00:00:06,640 --> 00:00:11,180 setting an item when we already know the index we want to set or retrieve. 4 00:00:11,180 --> 00:00:13,380 So, what are they not great for? 5 00:00:13,380 --> 00:00:17,800 The items in the array are stored in the computer's memory, one after another. 6 00:00:17,800 --> 00:00:21,880 Right after the end of an array, the computer can store other data. 7 00:00:21,880 --> 00:00:25,410 This is in order to make the most use of the limited memory space that 8 00:00:25,410 --> 00:00:26,860 a computer has. 9 00:00:26,860 --> 00:00:29,850 This is also why arrays have a fixed length. 10 00:00:29,850 --> 00:00:33,070 In order to store data right after the end of the array, 11 00:00:33,070 --> 00:00:36,149 the computer needs to know how long the array is. 12 00:00:37,160 --> 00:00:41,680 Once we've created an array of a certain length, we can't change our minds and add 13 00:00:41,680 --> 00:00:46,400 more space to the end of it, because we'd be stomping on data that's already there. 14 00:00:46,400 --> 00:00:50,120 So what do we do if we have an array that's three items long and 15 00:00:50,120 --> 00:00:52,600 we want to add a fourth item to the end? 16 00:00:52,600 --> 00:00:56,430 The array is full, so there's no room to put more items. 17 00:00:56,430 --> 00:01:00,680 Similarly, what if we want to insert an item at the beginning of an array or 18 00:01:00,680 --> 00:01:01,440 somewhere in the middle? 19 00:01:02,720 --> 00:01:05,230 Let's investigate this problem with some code. 20 00:01:06,440 --> 00:01:13,483 Let's create an array of three integers to keep track of people's ages. 21 00:01:13,483 --> 00:01:21,137 So I'll say int[] ages = { 24, 31, 56}. 22 00:01:21,137 --> 00:01:25,070 Now let's attempt to add another age to the end of the array. 23 00:01:25,070 --> 00:01:33,226 So I'll say ages[]ages.Length = 64. 24 00:01:33,226 --> 00:01:36,400 We get a System.IndexOutOfRangeException, 25 00:01:36,400 --> 00:01:41,500 because the array is only three items long and we're trying to add a fourth. 26 00:01:41,500 --> 00:01:46,100 What if we wanted to add another age, but keep the ages in the array sorted? 27 00:01:46,100 --> 00:01:51,280 So if we wanted to add 16, we'd need to add it to the beginning of the array at 28 00:01:51,280 --> 00:01:56,940 index 0, but 24 is already at index 0 here. 29 00:01:56,940 --> 00:02:02,500 We'd also be adding a fourth item to an array that only has room for three. 30 00:02:02,500 --> 00:02:07,460 The only way to add a fourth item is to create a larger array and 31 00:02:07,460 --> 00:02:11,020 copy everything from our original array to the new array. 32 00:02:11,020 --> 00:02:14,650 Every array inherits from a base array class. 33 00:02:14,650 --> 00:02:18,370 Let's take a look at the array class documentation. 34 00:02:18,370 --> 00:02:22,320 You can find this by searching for system data array, or 35 00:02:22,320 --> 00:02:24,150 just click on the link in the teacher's notes. 36 00:02:28,456 --> 00:02:32,720 This class contains many methods that can help us deal with the race. 37 00:02:34,763 --> 00:02:37,380 The method we want is the CopyTo method. 38 00:02:38,930 --> 00:02:42,411 So scroll down here and find CopyTo. 39 00:02:44,656 --> 00:02:45,550 Here we go. 40 00:02:45,550 --> 00:02:49,900 It says, copies all the elements of the current one dimensional array 41 00:02:49,900 --> 00:02:54,170 to the specified one dimensional array, starting at the specified array index. 42 00:02:55,390 --> 00:03:00,350 So first we need to create a new array that is the size we want. 43 00:03:00,350 --> 00:03:04,822 So I'll say int[] 44 00:03:04,822 --> 00:03:11,140 agesCopy = new int[4]. 45 00:03:11,140 --> 00:03:16,000 Now we can copy everything from ages into agesCopy. 46 00:03:16,000 --> 00:03:20,630 If we want to insert an age at the begging of our array, then we'll want to put 47 00:03:20,630 --> 00:03:27,200 the new items starting at index one of agesCopy. 48 00:03:27,200 --> 00:03:34,213 So I'll say ages.CopyTo(agesCopy, and we'll start at index 1. 49 00:03:34,213 --> 00:03:39,529 Now if we look at the contents of agesCopy, we'll see that our three 50 00:03:39,529 --> 00:03:44,870 items are in there and we have a space to put a fourth item at index 0. 51 00:03:44,870 --> 00:03:49,819 So now I can say, agesCopy[0] = 16. 52 00:03:55,654 --> 00:03:58,867 We do something similar if we wanted to add an item at the end or 53 00:03:58,867 --> 00:04:01,730 somewhere in the middle of the array. 54 00:04:01,730 --> 00:04:05,510 As you can see, it's not easy to add an item to a full array. 55 00:04:06,670 --> 00:04:11,940 Finally, we want to overwrite the ages array with the agesCopy array. 56 00:04:11,940 --> 00:04:14,850 However, we need to be careful here. 57 00:04:14,850 --> 00:04:18,460 Consider the situation where another class in our program 58 00:04:18,460 --> 00:04:21,750 already has a reference to the ages array. 59 00:04:21,750 --> 00:04:27,090 Remember this ages here is only a variable that refers to the ages array. 60 00:04:28,260 --> 00:04:32,720 Even if we set ages equal to agesCopy, other parts of 61 00:04:32,720 --> 00:04:37,640 the program that are referring to this array won't see this change. 62 00:04:37,640 --> 00:04:40,080 We've only changed one variable. 63 00:04:40,080 --> 00:04:44,591 If If we wanted to make it so the all parts of our program that we're referring 64 00:04:44,591 --> 00:04:49,099 to the original array are now referring to our new array, we'd need to go and 65 00:04:49,099 --> 00:04:50,950 reset all of those variables. 66 00:04:52,450 --> 00:04:56,780 This seems like a lot of trouble just to add one more item to an array 67 00:04:56,780 --> 00:05:00,310 not to mention it's very slow to add just one item. 68 00:05:00,310 --> 00:05:01,830 Creating a second array and 69 00:05:01,830 --> 00:05:06,160 copying everything every time we want to add an item is very inefficient and 70 00:05:06,160 --> 00:05:08,570 can slow down a computer program if we do this a lot. 71 00:05:09,730 --> 00:05:12,870 We also run into the same sort of troubles when we want to remove 72 00:05:12,870 --> 00:05:14,200 items from an array. 73 00:05:14,200 --> 00:05:18,280 In that case we need to shift all of the items after the removed item 74 00:05:18,280 --> 00:05:21,160 over by one to fill in the missing index. 75 00:05:21,160 --> 00:05:26,310 As you can imagine, this makes removing items from an array very slow. 76 00:05:26,310 --> 00:05:28,990 Although arrays are very fast for retrieving and 77 00:05:28,990 --> 00:05:32,970 setting items, they're the slowest collection for adding or removing items. 78 00:05:34,190 --> 00:05:36,670 Due to the difficulty in adding, removing and 79 00:05:36,670 --> 00:05:41,360 resizing, arrays are rarely used directly like this in C#. 80 00:05:41,360 --> 00:05:45,370 Instead there's a collection type that works just like an array, but 81 00:05:45,370 --> 00:05:48,070 it takes care of all this copying for us. 82 00:05:48,070 --> 00:05:51,160 This makes it easier to add and remove items. 83 00:05:51,160 --> 00:05:52,780 This collection is called a list.