1 00:00:00,680 --> 00:00:01,700 ‫Welcome back. 2 00:00:01,730 --> 00:00:07,670 ‫Now that you're a pro in a race, let's go ahead and create a 2D array or in general, multi-dimensional 3 00:00:07,670 --> 00:00:12,360 ‫arrays, because arrays cannot just be one line of entries. 4 00:00:12,380 --> 00:00:17,860 ‫They can be two dimensional, three dimensional or even multi dimensional further on. 5 00:00:17,870 --> 00:00:24,690 ‫So let's create a very simple 2D array and I'm going to create a string to the array. 6 00:00:24,710 --> 00:00:31,760 ‫And what I need to do here is instead of leaving the square brackets empty, I enter a comma in there 7 00:00:31,760 --> 00:00:35,470 ‫or I put a comma in there, and afterwards I give it a name. 8 00:00:35,480 --> 00:00:38,040 ‫So this would be an example for a matrix. 9 00:00:38,060 --> 00:00:43,610 ‫So we have a 2D array which is of type string and it's called matrix. 10 00:00:43,610 --> 00:00:50,330 ‫So that's how I can declare a 2D array, declare to the array. 11 00:00:50,990 --> 00:00:55,180 ‫So now if I want to declare a 3D array, it's pretty simple as well. 12 00:00:55,190 --> 00:00:56,150 ‫Three D array. 13 00:00:57,500 --> 00:01:01,610 ‫Now I do it with integers just so you see that it works otherwise as well. 14 00:01:01,610 --> 00:01:04,100 ‫And I'm going to call that down three D. 15 00:01:04,850 --> 00:01:07,300 ‫So that's how you can create a3d array. 16 00:01:07,310 --> 00:01:10,040 ‫And as you can see, it's based on the commas. 17 00:01:10,040 --> 00:01:16,940 ‫So one comma, two D, two commas, three D, four commas, five D and so forth. 18 00:01:17,030 --> 00:01:22,880 ‫I did skip the three commas, which would be 40, but that's how you can simply create multi dimensional 19 00:01:22,880 --> 00:01:23,560 ‫arrays. 20 00:01:23,570 --> 00:01:25,790 ‫Well, that's how we can declare them. 21 00:01:25,790 --> 00:01:28,040 ‫Now let's have a look at how to initialize them. 22 00:01:28,040 --> 00:01:35,480 ‫So I'm going to create a new two dimensional array, two dimensional array, and I'm going to call that 23 00:01:35,480 --> 00:01:41,060 ‫one int array, two DX. 24 00:01:41,990 --> 00:01:43,330 ‫I'm going to keep it simple. 25 00:01:43,340 --> 00:01:52,280 ‫And in order to create it, I need to use a new then the data type again. 26 00:01:52,280 --> 00:01:57,130 ‫And here I need to declare how many dimensions we have. 27 00:01:57,140 --> 00:02:01,190 ‫So in this case one dimension more than to general one. 28 00:02:01,190 --> 00:02:05,480 ‫So it's two dimensions in total, and then I need two curly brackets. 29 00:02:05,480 --> 00:02:11,420 ‫And what I need to do here now is to go ahead and enter the different rows. 30 00:02:11,420 --> 00:02:21,890 ‫So I'm going to create a row of one, two and three separate by comma, four, five and six, separate 31 00:02:21,890 --> 00:02:26,210 ‫by comma and seven, eight and nine. 32 00:02:26,990 --> 00:02:31,460 ‫So that would be an example of a two DX array, which is a matrix. 33 00:02:31,460 --> 00:02:33,470 ‫So pretty much the same thing that we had here. 34 00:02:33,950 --> 00:02:39,470 ‫And I start off with one even though the array itself starts with zero. 35 00:02:39,470 --> 00:02:44,120 ‫So in order to access this one, I would need to do the following. 36 00:02:45,560 --> 00:02:47,630 ‫I can simply write it on the console. 37 00:02:47,630 --> 00:02:50,060 ‫So see w double tap. 38 00:02:50,750 --> 00:02:53,690 ‫And actually what I want to access is the five. 39 00:02:53,690 --> 00:02:56,060 ‫It's much more interesting, I'd say. 40 00:02:56,630 --> 00:03:11,570 ‫So I'm going to say central value is and then it's going to be array two DX and then in the square brackets 41 00:03:11,570 --> 00:03:14,590 ‫I enter which positions I want to access. 42 00:03:14,600 --> 00:03:20,450 ‫So in my case I want to access the position one and one. 43 00:03:21,410 --> 00:03:22,760 ‫Why is it one and one? 44 00:03:22,760 --> 00:03:25,640 ‫Well, that's row number zero. 45 00:03:25,640 --> 00:03:27,260 ‫That's row number one. 46 00:03:27,260 --> 00:03:30,080 ‫So this is the row one that I want to access. 47 00:03:30,080 --> 00:03:36,290 ‫So this one here and that one is index one within that row. 48 00:03:36,410 --> 00:03:42,290 ‫So this four is index zero, five is index one and six is index two. 49 00:03:43,160 --> 00:03:51,200 ‫So you could say row zero here, row one and here, row two. 50 00:03:52,660 --> 00:03:52,970 ‫All right. 51 00:03:52,970 --> 00:03:54,440 ‫So that's pretty much what we do here. 52 00:03:54,440 --> 00:03:57,680 ‫And then zero, one, two and so forth. 53 00:03:57,680 --> 00:04:02,810 ‫So based on how many entries you have here, this is how you can simply access it. 54 00:04:02,810 --> 00:04:04,550 ‫So now let's check it out. 55 00:04:04,730 --> 00:04:12,530 ‫Let's read a key so that it doesn't disappear and we are central value is five. 56 00:04:12,950 --> 00:04:13,280 ‫All right. 57 00:04:13,280 --> 00:04:16,700 ‫So this is how you can access the five now for you as a little challenge. 58 00:04:16,700 --> 00:04:19,010 ‫Try to access the seven. 59 00:04:21,380 --> 00:04:21,650 ‫All right. 60 00:04:21,650 --> 00:04:22,780 ‫I hope you tried it. 61 00:04:22,790 --> 00:04:26,660 ‫So in order to access seven, I need to use row two. 62 00:04:26,840 --> 00:04:30,590 ‫And then which index is it at zero. 63 00:04:30,590 --> 00:04:32,990 ‫So to zero is going to be our seven. 64 00:04:32,990 --> 00:04:34,430 ‫So let's run it again. 65 00:04:34,910 --> 00:04:36,770 ‫Central value is seven. 66 00:04:37,700 --> 00:04:38,370 ‫All righty. 67 00:04:38,870 --> 00:04:39,830 ‫So there we are. 68 00:04:39,860 --> 00:04:46,280 ‫This is how you can use it to DX array if you want to run a3d array, that's a bit more complex in terms 69 00:04:46,280 --> 00:04:50,780 ‫well, it adds a whole dimension, but the concept is the same. 70 00:04:50,870 --> 00:04:56,390 ‫So then you would have well, I'm just going to create a very simple three d array, just so you see. 71 00:04:56,990 --> 00:04:57,350 ‫All right. 72 00:04:57,350 --> 00:05:00,110 ‫So I went ahead and I created this three D array. 73 00:05:00,110 --> 00:05:05,600 ‫So you have the curly brackets, which is the first dimension, then curly brackets again second dimension, 74 00:05:05,600 --> 00:05:09,950 ‫and then the curly brackets again, which is the third dimension. 75 00:05:09,950 --> 00:05:20,690 ‫So you have your depth, your height and your length pretty much if you see it as a3d object, for example. 76 00:05:20,980 --> 00:05:21,260 ‫Right? 77 00:05:21,260 --> 00:05:27,290 ‫So if I would like to access those, I use string in this case because it's easier to demonstrate it. 78 00:05:27,290 --> 00:05:34,100 ‫So if I would like to access this first number, I would have to use 000 in order to get that. 79 00:05:34,100 --> 00:05:37,640 ‫So let me show you that this is in fact true. 80 00:05:37,670 --> 00:05:40,340 ‫So I'm just going to copy those two lines. 81 00:05:40,880 --> 00:05:42,410 ‫Actually, I'm going to cut them out. 82 00:05:42,500 --> 00:05:52,880 ‫And I want to get not the central value, but the value is and I'm just going to use here the three 83 00:05:52,880 --> 00:06:05,390 ‫D array every three D and I want to access position one, one and zero just as an example. 84 00:06:05,420 --> 00:06:06,560 ‫Let's run this. 85 00:06:07,290 --> 00:06:09,540 ‫And as you can see, the value is 110. 86 00:06:09,540 --> 00:06:15,960 ‫So that might be rather well, weird or simple because you'd say, okay, this is what you entered here. 87 00:06:15,960 --> 00:06:16,810 ‫110. 88 00:06:16,830 --> 00:06:23,010 ‫Yeah, that's because I named the entries the exact same way that I need in order to access them. 89 00:06:23,700 --> 00:06:30,240 ‫So let's say I add another row here and I'm going to use zero. 90 00:06:30,840 --> 00:06:32,610 ‫I'm just going to call that one. 91 00:06:34,260 --> 00:06:35,310 ‫Hi there. 92 00:06:36,030 --> 00:06:39,810 ‫And what's up? 93 00:06:41,880 --> 00:06:43,680 ‫So this is pretty simple. 94 00:06:44,400 --> 00:06:46,170 ‫Nothing has changed. 95 00:06:48,180 --> 00:06:51,390 ‫Except for this one here. 96 00:06:51,390 --> 00:06:56,880 ‫As you can see, this array here is not happy anymore or this piece of the three D arrays are not happy 97 00:06:56,880 --> 00:07:01,260 ‫anymore because here we had three rows and here we only have two rows. 98 00:07:01,260 --> 00:07:10,350 ‫So I need to add another one and here another one and last entry. 99 00:07:11,130 --> 00:07:12,510 ‫So just call them that way. 100 00:07:12,510 --> 00:07:18,360 ‫So if I now let's say I want to access this high there actually try to do that yourself. 101 00:07:18,450 --> 00:07:21,210 ‫Try to enter the correct value in order to access. 102 00:07:21,210 --> 00:07:21,900 ‫Hi there. 103 00:07:22,960 --> 00:07:25,390 ‫I mean in here, access it. 104 00:07:27,300 --> 00:07:27,690 ‫All right. 105 00:07:27,690 --> 00:07:30,390 ‫So we said that this is zero. 106 00:07:30,750 --> 00:07:31,920 ‫At the first point. 107 00:07:32,710 --> 00:07:35,710 ‫This is zero one, actually zero zero here. 108 00:07:35,740 --> 00:07:41,500 ‫This row this row is zero one, and this row should be zero two then. 109 00:07:41,500 --> 00:07:45,310 ‫So 020 will be high there. 110 00:07:45,910 --> 00:07:51,790 ‫So let's access that 020 and let's run it. 111 00:07:53,050 --> 00:07:56,020 ‫And as you can see here, it says the value is high there. 112 00:07:56,680 --> 00:08:00,400 ‫If I want to access last entry, same approach. 113 00:08:00,760 --> 00:08:02,820 ‫This row is one zero. 114 00:08:02,830 --> 00:08:05,230 ‫This row here is one one. 115 00:08:05,230 --> 00:08:08,740 ‫So this row here will be one two. 116 00:08:09,040 --> 00:08:14,110 ‫So we access one, two and one in order to get last entry. 117 00:08:14,590 --> 00:08:17,830 ‫So let's run it and we see the values last entry. 118 00:08:18,440 --> 00:08:19,270 ‫Okeydokey. 119 00:08:19,270 --> 00:08:21,880 ‫So I could go deeper into that. 120 00:08:21,880 --> 00:08:27,400 ‫But I think the principle is clear and this is simply how you can create a3d array. 121 00:08:27,640 --> 00:08:34,210 ‫And by the way, there are different ways to create multiple dimension arrays. 122 00:08:34,300 --> 00:08:40,090 ‫For example, I could also specify the dimensions earlier on, right? 123 00:08:40,090 --> 00:08:54,130 ‫So I could say for example and array I'm going to call that one actually string array to D string is 124 00:08:54,130 --> 00:08:56,140 ‫a new string. 125 00:08:56,140 --> 00:09:06,040 ‫And here I need to specify, for example that I want to have three rows and two entries per row and 126 00:09:06,340 --> 00:09:20,470 ‫that one will be, for example, one, then we have two, then we have three, four and so forth, which 127 00:09:20,470 --> 00:09:25,210 ‫means and here we have five and six. 128 00:09:25,210 --> 00:09:27,250 ‫So that's another way to do it, right? 129 00:09:27,550 --> 00:09:35,830 ‫So I can specify it at that point here so that I need three rows, which is going to be that row, that 130 00:09:35,830 --> 00:09:37,840 ‫row here and that row. 131 00:09:39,650 --> 00:09:41,270 ‫And actually like this. 132 00:09:43,040 --> 00:09:47,030 ‫So that's how I can specify how many entries I want to have. 133 00:09:47,130 --> 00:09:47,720 ‫Right. 134 00:09:49,220 --> 00:09:55,070 ‫Now for you as a little challenge try to change the for to chicken. 135 00:09:55,550 --> 00:09:58,100 ‫Just change it to chicken, even if you're a vegan. 136 00:10:00,020 --> 00:10:00,410 ‫All right. 137 00:10:00,410 --> 00:10:01,520 ‫I hope you tried it. 138 00:10:01,670 --> 00:10:04,000 ‫So let's try that. 139 00:10:04,010 --> 00:10:10,220 ‫It's going to be array, string or array to the string at the position. 140 00:10:11,120 --> 00:10:12,200 ‫And let's check. 141 00:10:12,200 --> 00:10:14,120 ‫This is row zero. 142 00:10:14,150 --> 00:10:16,760 ‫This is row one and four. 143 00:10:16,760 --> 00:10:18,950 ‫Should be one and one then. 144 00:10:19,460 --> 00:10:21,740 ‫And I'm going to change that to chicken. 145 00:10:22,580 --> 00:10:26,510 ‫And now let's see the value. 146 00:10:27,310 --> 00:10:33,490 ‫So I say array two d string at the position of one and one. 147 00:10:34,520 --> 00:10:37,570 ‫And now let's see if we get our chicken. 148 00:10:37,570 --> 00:10:38,920 ‫The value is chicken. 149 00:10:39,550 --> 00:10:40,020 ‫Great. 150 00:10:40,090 --> 00:10:46,030 ‫So now you can see how you can change the value of an entry within a two array. 151 00:10:46,510 --> 00:10:53,890 ‫So let's say you have an array or you have a name of an array and you don't know how many dimensions 152 00:10:53,890 --> 00:10:54,520 ‫it has. 153 00:10:54,670 --> 00:10:58,300 ‫So in order to find out the dimensions, let's go ahead and do that. 154 00:10:58,960 --> 00:11:03,670 ‫Actually, I'm going to create a new variable. 155 00:11:04,720 --> 00:11:07,420 ‫I'm going to call it in dimensions. 156 00:11:07,420 --> 00:11:12,820 ‫And it's going to be array to d string that rank. 157 00:11:13,690 --> 00:11:19,270 ‫And what rank does is get the rank, the number of dimensions of the array. 158 00:11:19,840 --> 00:11:25,310 ‫So if one dimensional array returns one that a two dimensional array should return to. 159 00:11:25,330 --> 00:11:32,220 ‫So dimensions now should be two in this case because the 2D string has two dimensions. 160 00:11:32,230 --> 00:11:37,980 ‫So I'm just going to print the dimensions out now and let's run that. 161 00:11:37,990 --> 00:11:39,680 ‫And it says the value is two. 162 00:11:39,700 --> 00:11:46,630 ‫So in case you want to figure out which amount of dimensions a variable has that you get, for example, 163 00:11:46,630 --> 00:11:51,160 ‫in this case it's simple, but in more complex cases, you could get that as an argument. 164 00:11:51,160 --> 00:11:55,750 ‫And you well, maybe your program runs with different dimensions. 165 00:11:55,750 --> 00:12:01,210 ‫It accepts multiple different dimensions, not just the two dimensional array, maybe also a three dimensional 166 00:12:01,210 --> 00:12:02,170 ‫array and so forth. 167 00:12:02,170 --> 00:12:06,330 ‫And you would like to know how many dimensions the array has before you work with it. 168 00:12:06,340 --> 00:12:08,350 ‫So that's how you could figure it out. 169 00:12:10,270 --> 00:12:15,640 ‫By the way, you can also create an array without specifying the rank. 170 00:12:15,640 --> 00:12:19,070 ‫So in our cases we have always specified the rank. 171 00:12:19,090 --> 00:12:22,540 ‫And here we said, okay, we have two dimensions. 172 00:12:22,540 --> 00:12:26,350 ‫We have three dimensions and so forth. 173 00:12:27,520 --> 00:12:32,320 ‫But what we did that here as well, we need to do that still on that side, but we don't need to do 174 00:12:32,320 --> 00:12:35,670 ‫it on the right hand side of our equal sign. 175 00:12:35,680 --> 00:12:42,400 ‫So I'm going to show you a little example inch and it's going to be an array two DX and I'm going to 176 00:12:42,400 --> 00:12:43,120 ‫call it too. 177 00:12:43,120 --> 00:12:45,420 ‫So it's just going to be a different array. 178 00:12:45,430 --> 00:12:54,310 ‫And in here I can simply use the square or the curly brackets here to create the array. 179 00:12:54,460 --> 00:13:00,910 ‫So as you can see, I have a two dx array now it doesn't complain and I didn't need to use this new 180 00:13:00,910 --> 00:13:04,750 ‫string or new end kind of thing where a specified the rank. 181 00:13:05,980 --> 00:13:07,690 ‫So it automatically knows. 182 00:13:07,690 --> 00:13:08,060 ‫Okay. 183 00:13:08,200 --> 00:13:12,250 ‫It's a tree rank by this declaration here. 184 00:13:13,420 --> 00:13:18,000 ‫However, if you have already declared an array. 185 00:13:18,010 --> 00:13:21,370 ‫So let's say we have our matrix, we have declared it here. 186 00:13:21,640 --> 00:13:25,420 ‫I cannot go ahead and let me actually do it here. 187 00:13:25,690 --> 00:13:35,620 ‫I cannot just go ahead and say metrics is going to be it's a one to. 188 00:13:37,240 --> 00:13:40,440 ‫And it's around curly brackets here as well. 189 00:13:40,450 --> 00:13:43,600 ‫So this is something that you should not do. 190 00:13:44,230 --> 00:13:46,380 ‫Now, this is going to return an error. 191 00:13:46,390 --> 00:13:48,220 ‫It doesn't accept that approach. 192 00:13:48,760 --> 00:13:49,120 ‫All right. 193 00:13:49,120 --> 00:13:51,280 ‫So don't do that, just so you know. 194 00:13:51,280 --> 00:13:56,800 ‫But when you create it, you can initialize it that way straight away. 195 00:13:57,280 --> 00:13:57,700 ‫All right. 196 00:13:57,700 --> 00:14:02,980 ‫So that was it on multi dimensional arrays in the next video, that will be a little challenge where 197 00:14:02,980 --> 00:14:05,950 ‫you actually will need to use two dimensional arrays. 198 00:14:05,950 --> 00:14:12,280 ‫We're going to create a little tic tac toe clone, and you should try to build that yourself in the 199 00:14:12,280 --> 00:14:12,760 ‫next video. 200 00:14:12,760 --> 00:14:19,900 ‫So be prepared and make sure that you understand two dimensional arrays, how to use them, how to access 201 00:14:19,900 --> 00:14:21,820 ‫elements and so forth. 202 00:14:22,330 --> 00:14:22,780 ‫All right. 203 00:14:22,780 --> 00:14:24,250 ‫So see you in the next video.