1 00:00:00,300 --> 00:00:11,370 Let's talk arrays or collections of stuff in Swift, let's create one var my array is equal to two square 2 00:00:11,370 --> 00:00:17,610 brackets and then whatever numbers you like in those square brackets, separated by commas. 3 00:00:19,280 --> 00:00:25,820 So if I go to the next line and I type my ah, let's see what kind of thing it is. 4 00:00:28,960 --> 00:00:36,610 My array of integers, so an array is a collection of the same kind of thing, in this case integers, 5 00:00:37,120 --> 00:00:42,820 that's as big or as small as you want it to be, but it's a way of stacking together similar types of 6 00:00:42,820 --> 00:00:46,900 variables that you don't want to create lots of variables for, obviously. 7 00:00:48,150 --> 00:00:53,710 The interesting thing actually about arrays is that they are stored sequentially in memory with most 8 00:00:53,710 --> 00:00:54,340 languages. 9 00:00:54,550 --> 00:01:00,070 So if I want the value five and six, they actually sit next to each other, at least logically in the 10 00:01:00,070 --> 00:01:01,610 memory, perhaps not physically. 11 00:01:01,630 --> 00:01:07,090 I haven't looked into it that far, but they sit next to each other as far as memory addresses go. 12 00:01:07,810 --> 00:01:12,290 OK, so that is an array, a collection of like minded items. 13 00:01:12,700 --> 00:01:19,360 Now we can explicitly tell the system what kind of array we want so we can have vah and we can tell 14 00:01:19,360 --> 00:01:19,620 it. 15 00:01:19,630 --> 00:01:27,880 I want right now an array of type string and then I'm going to give it an array of strings. 16 00:01:29,490 --> 00:01:30,300 Just two of them. 17 00:01:31,140 --> 00:01:31,740 Hi there. 18 00:01:32,970 --> 00:01:38,220 So there are two strings in that array, and we've told the system this is an array of strings, of 19 00:01:38,220 --> 00:01:41,530 course, we could potentially do the same up top here. 20 00:01:42,090 --> 00:01:46,380 We say this is an array of, you guessed it, integers. 21 00:01:48,830 --> 00:01:52,990 And as long as it's getting what it wants, then we're all good if I put five point eight in here. 22 00:01:54,230 --> 00:02:00,020 We should get a nice red arrow because I've tried to insert a double rainbow, five point eight is not 23 00:02:00,020 --> 00:02:00,630 an integer. 24 00:02:00,950 --> 00:02:05,300 Now, some languages would actually take that five point eight, drop the point eight and just give 25 00:02:05,300 --> 00:02:08,930 you five, which is an awful idea as far as languages go. 26 00:02:10,310 --> 00:02:14,220 Okay, so what else have we got on my notes here via Mayan's? 27 00:02:14,660 --> 00:02:20,840 This is another way of declaring your array is equal to an array of integers. 28 00:02:23,060 --> 00:02:29,660 Open and closed brackets, so in this case, there are zero elements in this array which are saying 29 00:02:29,900 --> 00:02:35,720 just initialize it so it's ready for me to use because I don't have anything to put inside it just yet. 30 00:02:36,800 --> 00:02:38,330 Now, how do you put things inside? 31 00:02:38,360 --> 00:02:43,710 Well, we go to my aunt's dot and you'll notice there are lots of functions we can use here. 32 00:02:44,150 --> 00:02:46,940 We can append the number eight. 33 00:02:47,450 --> 00:02:48,710 So now it adds the number eight. 34 00:02:49,100 --> 00:02:53,900 We can have my aunts dot append the number two or one. 35 00:02:54,470 --> 00:02:55,460 And if we run that. 36 00:02:56,470 --> 00:02:58,150 We should get nothing. 37 00:02:59,100 --> 00:03:04,380 Eight and then eight and one as the elements of this particular array. 38 00:03:05,680 --> 00:03:08,380 How do we access the items in the array? 39 00:03:08,410 --> 00:03:10,270 Well, that is very straightforward. 40 00:03:10,300 --> 00:03:11,290 We have my int. 41 00:03:12,460 --> 00:03:19,030 We open the square brackets and we give it the index, so the first element in an array is always element 42 00:03:19,030 --> 00:03:19,650 zero. 43 00:03:20,080 --> 00:03:23,320 Everything in programming is zero based indexing. 44 00:03:23,560 --> 00:03:25,240 You start counting at zero. 45 00:03:25,810 --> 00:03:28,870 So if I run, this zero will be the element of eight. 46 00:03:30,210 --> 00:03:36,360 Now, of course, that doesn't spit out an array that's giving us an integer because we've gone away, 47 00:03:36,390 --> 00:03:41,940 we've dove into the array already and we're pulling out that individual item, whatever type it is, 48 00:03:41,940 --> 00:03:43,430 in this case, an integer. 49 00:03:44,520 --> 00:03:44,850 Right. 50 00:03:44,860 --> 00:03:48,960 So your homework, nice and simple, perhaps. 51 00:03:50,930 --> 00:03:56,490 Create a three element array of numbers, multiply all of them. 52 00:03:57,200 --> 00:04:00,650 Now the tasks are starting to get a little bit harder. 53 00:04:00,830 --> 00:04:06,160 If you're new to all of this, so sit down and just think about how you'd go through this. 54 00:04:06,530 --> 00:04:08,300 Don't worry about the code. 55 00:04:09,380 --> 00:04:14,090 What I'd like you to do is think conceptually about it, writing a piece of paper on your iPad, on 56 00:04:14,090 --> 00:04:17,750 your phone, how you think these should be laid out. 57 00:04:18,140 --> 00:04:25,460 A lot of code, a lot of problems that we need to solve in code are solved by logical thinking, not 58 00:04:25,700 --> 00:04:26,810 by knowing code. 59 00:04:27,170 --> 00:04:33,050 Yes, it's partly knowing code, but most of the work is done in just thinking logically, first of 60 00:04:33,050 --> 00:04:33,220 all. 61 00:04:33,230 --> 00:04:35,990 So give that a go and I'll see you in the next one.