1 00:00:00,930 --> 00:00:02,760 ‫Instructor: Hi, within this lecture, 2 00:00:02,760 --> 00:00:07,760 ‫we are going to learn a data structure called arrays. 3 00:00:07,860 --> 00:00:10,800 ‫So we're gonna see what an array is 4 00:00:10,800 --> 00:00:13,290 ‫and we're going to see some of the occasions, 5 00:00:13,290 --> 00:00:17,070 ‫that we may want to use arrays insight. 6 00:00:17,070 --> 00:00:21,180 ‫So over here we have our main class, 7 00:00:21,180 --> 00:00:24,840 ‫but I believe we have a lot of information, 8 00:00:24,840 --> 00:00:26,430 ‫inside of this class. 9 00:00:26,430 --> 00:00:29,010 ‫So in order to be more structural, 10 00:00:29,010 --> 00:00:32,250 ‫I'm going to change this name, okay? 11 00:00:32,250 --> 00:00:35,040 ‫So it's called Main right now, 12 00:00:35,040 --> 00:00:37,860 ‫I can call this anything I want. 13 00:00:37,860 --> 00:00:40,380 ‫All I have to do is just select this 14 00:00:40,380 --> 00:00:43,440 ‫and right click it to change its name. 15 00:00:43,440 --> 00:00:47,220 ‫And of course if you're doing that with an activity, 16 00:00:47,220 --> 00:00:50,880 ‫with another project, you can just close this down 17 00:00:50,880 --> 00:00:54,210 ‫and create a new project for you at this point. 18 00:00:54,210 --> 00:00:56,160 ‫But I'm just going to change this name, 19 00:00:56,160 --> 00:01:00,180 ‫by saying refactor and rename, okay? 20 00:01:00,180 --> 00:01:03,990 ‫Because there is no point calling that main. 21 00:01:03,990 --> 00:01:05,850 ‫I'm going to call this variables, 22 00:01:05,850 --> 00:01:09,300 ‫because that's what we have covered here, right? 23 00:01:09,300 --> 00:01:11,220 ‫So I'm going to say variables, 24 00:01:11,220 --> 00:01:14,700 ‫with a upper case V and hit enter. 25 00:01:14,700 --> 00:01:17,217 ‫As you can see, as I type over here, 26 00:01:17,217 --> 00:01:21,900 ‫it changes the file name as well. 27 00:01:21,900 --> 00:01:24,120 ‫So if I run this right now, 28 00:01:24,120 --> 00:01:29,120 ‫I will get the same output, but this is okay for variables. 29 00:01:29,370 --> 00:01:33,150 ‫Now I'm going to create a new Java class. 30 00:01:33,150 --> 00:01:35,400 ‫So with inside of this class, 31 00:01:35,400 --> 00:01:38,010 ‫we are gonna go and see arrays. 32 00:01:38,010 --> 00:01:39,293 ‫So I'm going to name this array 33 00:01:39,293 --> 00:01:43,110 ‫and I'm just doing this for to be structural, 34 00:01:43,110 --> 00:01:45,270 ‫I could have just written it down, 35 00:01:45,270 --> 00:01:48,810 ‫in the same variables part as well, 36 00:01:48,810 --> 00:01:51,990 ‫but it's better to write it in here. 37 00:01:51,990 --> 00:01:54,330 ‫So of course, within this class, 38 00:01:54,330 --> 00:01:58,500 ‫we have to have that main function again. 39 00:01:58,500 --> 00:02:00,810 ‫So I'm going to create it first, 40 00:02:00,810 --> 00:02:03,270 ‫public static void, main 41 00:02:03,270 --> 00:02:06,840 ‫and remember, you have to open and close parentheses, 42 00:02:06,840 --> 00:02:09,270 ‫inside of this main method 43 00:02:09,270 --> 00:02:12,690 ‫and this is the entry point for our Java code. 44 00:02:12,690 --> 00:02:15,600 ‫So we need to write it exactly like this, 45 00:02:15,600 --> 00:02:18,240 ‫with a string array, by the way, this an array, 46 00:02:18,240 --> 00:02:20,430 ‫we are going to see what it is right now 47 00:02:20,430 --> 00:02:23,790 ‫and we're going to open and close curly braces, 48 00:02:23,790 --> 00:02:25,890 ‫not with a C inside. 49 00:02:25,890 --> 00:02:26,723 ‫Here we go. 50 00:02:26,723 --> 00:02:28,860 ‫Now we have our entry point. 51 00:02:28,860 --> 00:02:32,193 ‫Now we can write whatever we want inside of this method. 52 00:02:33,030 --> 00:02:34,680 ‫So what is an array? 53 00:02:34,680 --> 00:02:39,390 ‫Array is a data structure to store multiple values. 54 00:02:39,390 --> 00:02:42,300 ‫For example, you can store multiple strings, 55 00:02:42,300 --> 00:02:45,150 ‫you can store multiple in integers 56 00:02:45,150 --> 00:02:49,890 ‫or multiple floats or doubles inside of an array, okay? 57 00:02:49,890 --> 00:02:53,160 ‫You can store any data type that we have learned, 58 00:02:53,160 --> 00:02:55,680 ‫in the previous lectures. 59 00:02:55,680 --> 00:02:59,010 ‫So that you can actually reach them, 60 00:02:59,010 --> 00:03:01,380 ‫within a single variable. 61 00:03:01,380 --> 00:03:05,700 ‫Just imagine that you have thousand strings on the internet, 62 00:03:05,700 --> 00:03:06,960 ‫that you want to download. 63 00:03:06,960 --> 00:03:09,360 ‫So rather than creating a thousand variables, 64 00:03:09,360 --> 00:03:10,620 ‫for different strings, 65 00:03:10,620 --> 00:03:12,660 ‫you can create a string array 66 00:03:12,660 --> 00:03:15,150 ‫and then store everything inside of that array, 67 00:03:15,150 --> 00:03:19,020 ‫so that you can reach it any time you want. 68 00:03:19,020 --> 00:03:20,400 ‫Let's do that. 69 00:03:20,400 --> 00:03:23,790 ‫So I'm gonna write string, like if I, 70 00:03:23,790 --> 00:03:25,830 ‫as if I am creating a string, 71 00:03:25,830 --> 00:03:29,790 ‫but at the end of the type over here, 72 00:03:29,790 --> 00:03:33,090 ‫I'm gonna put this notation, okay? 73 00:03:33,090 --> 00:03:37,353 ‫This syntax, it means that I'm creating a string array, 74 00:03:38,370 --> 00:03:42,030 ‫so after the type, what do we write? 75 00:03:42,030 --> 00:03:45,540 ‫We write the name of the object, name of the variable, 76 00:03:45,540 --> 00:03:49,380 ‫like my string array, okay? 77 00:03:49,380 --> 00:03:53,793 ‫Then we can create, we can actually assign the value. 78 00:03:54,840 --> 00:03:57,060 ‫But before we go into the values, 79 00:03:57,060 --> 00:04:01,530 ‫there are a couple of ways to initialize string arrays 80 00:04:01,530 --> 00:04:05,040 ‫or any type of arrays that you may think of. 81 00:04:05,040 --> 00:04:08,790 ‫And I'm going to show you every one of them, don't worry. 82 00:04:08,790 --> 00:04:11,250 ‫First of all, we are gonna go for a traditional, 83 00:04:11,250 --> 00:04:15,390 ‫conventional way, which is initializing it, 84 00:04:15,390 --> 00:04:20,390 ‫by saying new string array like this, okay? 85 00:04:20,610 --> 00:04:22,980 ‫But as you can see, there is an error over here, 86 00:04:22,980 --> 00:04:27,980 ‫because it expects some parameter inside of this braces. 87 00:04:29,160 --> 00:04:31,770 ‫So for example, I'm gonna say three. 88 00:04:31,770 --> 00:04:34,650 ‫So what does this three represent? 89 00:04:34,650 --> 00:04:39,650 ‫It represents that there's going to be three elements, 90 00:04:39,660 --> 00:04:42,210 ‫inside of this my string array, 91 00:04:42,210 --> 00:04:45,540 ‫it will contain three strings. 92 00:04:45,540 --> 00:04:50,540 ‫So all we just did right now is to create a my string array 93 00:04:51,240 --> 00:04:54,690 ‫and said that it's going to be three elements, 94 00:04:54,690 --> 00:04:56,640 ‫inside of that string array. 95 00:04:56,640 --> 00:05:01,640 ‫If I say my string array right now, I can open this braces 96 00:05:02,010 --> 00:05:04,230 ‫and I can just start, 97 00:05:04,230 --> 00:05:09,030 ‫giving out the values over here like this. 98 00:05:09,030 --> 00:05:13,020 ‫So the first element inside of a string, 99 00:05:13,020 --> 00:05:15,660 ‫has the index of zero. 100 00:05:15,660 --> 00:05:19,410 ‫So whenever we say my array zero, 101 00:05:19,410 --> 00:05:23,370 ‫it means that first element in that array. 102 00:05:23,370 --> 00:05:27,330 ‫So in this case, I'm saying that James, 103 00:05:27,330 --> 00:05:31,530 ‫is the first element inside of my string array 104 00:05:31,530 --> 00:05:36,530 ‫and I can make that as long as I reach the capacity, 105 00:05:38,310 --> 00:05:43,310 ‫that I have given in the initialization part. 106 00:05:43,410 --> 00:05:46,290 ‫For example, I can go for Lars 107 00:05:46,290 --> 00:05:49,800 ‫and I can do this one more time, okay? 108 00:05:49,800 --> 00:05:53,610 ‫And I can just say something like, Kirk over here. 109 00:05:53,610 --> 00:05:57,570 ‫Now my string array has three elements 110 00:05:57,570 --> 00:06:00,693 ‫and let me try to write them down. 111 00:06:01,920 --> 00:06:06,920 ‫So if you do this, if you try to run this, okay? 112 00:06:07,740 --> 00:06:10,200 ‫Right click on main and say Run. 113 00:06:10,200 --> 00:06:14,160 ‫You won't see James, Lars, and Kirk like this. 114 00:06:14,160 --> 00:06:15,720 ‫You will see, yep, 115 00:06:15,720 --> 00:06:20,720 ‫this is a string array at this memory part, okay? 116 00:06:20,790 --> 00:06:24,540 ‫So in order to see the individual elements, 117 00:06:24,540 --> 00:06:28,830 ‫inside of an array, you have to specify, 118 00:06:28,830 --> 00:06:32,790 ‫the index of that particular element, 119 00:06:32,790 --> 00:06:34,650 ‫that you are trying to reach. 120 00:06:34,650 --> 00:06:36,960 ‫For example, like this, 121 00:06:36,960 --> 00:06:41,880 ‫if I say my string array zero, then I can see James. 122 00:06:41,880 --> 00:06:46,380 ‫If I wanna see Lars, then I can go for one. 123 00:06:46,380 --> 00:06:48,810 ‫And as you can see, it works. 124 00:06:48,810 --> 00:06:51,030 ‫Now I have a variable 125 00:06:51,030 --> 00:06:55,470 ‫and I have different values inside of that variable, 126 00:06:55,470 --> 00:06:59,043 ‫I can reach them by specifying their indexes. 127 00:07:00,090 --> 00:07:02,310 ‫So this works. 128 00:07:02,310 --> 00:07:07,310 ‫And again, if I had a 1000 array elements over here, 129 00:07:07,320 --> 00:07:09,603 ‫then it would work fine as well, 130 00:07:11,040 --> 00:07:13,830 ‫but maybe you can't think something like that. 131 00:07:13,830 --> 00:07:15,990 ‫Yeah, I had three elements over here 132 00:07:15,990 --> 00:07:17,400 ‫and I knew there's going, 133 00:07:17,400 --> 00:07:21,960 ‫there was going to be three elements, but what if I don't? 134 00:07:21,960 --> 00:07:24,870 ‫What if I don't know what kind of an element, 135 00:07:24,870 --> 00:07:26,130 ‫there is going to be over here? 136 00:07:26,130 --> 00:07:27,930 ‫Like how many elements? 137 00:07:27,930 --> 00:07:28,763 ‫I don't know. 138 00:07:28,763 --> 00:07:31,080 ‫I'm going to download it from the internet. 139 00:07:31,080 --> 00:07:33,840 ‫Maybe it'll be 900, maybe it'll be 1000. 140 00:07:33,840 --> 00:07:36,270 ‫How do I initialize this? 141 00:07:36,270 --> 00:07:37,103 ‫Don't worry. 142 00:07:37,103 --> 00:07:40,530 ‫We are gonna work on that later on. 143 00:07:40,530 --> 00:07:44,073 ‫Right now you just try to understand, 144 00:07:45,390 --> 00:07:48,390 ‫arrays are used in order to store values, 145 00:07:48,390 --> 00:07:53,390 ‫multiple values inside of one single object. 146 00:07:53,760 --> 00:07:55,920 ‫We can do that with integers as well. 147 00:07:55,920 --> 00:07:58,680 ‫For example, in my integer array, 148 00:07:58,680 --> 00:08:03,390 ‫will be new int array like this, okay? 149 00:08:03,390 --> 00:08:06,390 ‫You can just call it three for example, again 150 00:08:06,390 --> 00:08:10,260 ‫and you can add as many numbers as you want over here, 151 00:08:10,260 --> 00:08:13,500 ‫up until three elements like this, 152 00:08:13,500 --> 00:08:15,780 ‫my integer array zero 50, 153 00:08:15,780 --> 00:08:20,730 ‫my integer array one is something else like 60, 154 00:08:20,730 --> 00:08:25,730 ‫let's make 50, 60, and 70 with their related ages, 155 00:08:26,010 --> 00:08:30,420 ‫like James' age is 50, Lars' age is 60, 156 00:08:30,420 --> 00:08:33,390 ‫and Kirk's age is 70, okay? 157 00:08:33,390 --> 00:08:38,390 ‫So for example, if I reach out to second element, 158 00:08:39,630 --> 00:08:42,150 ‫index one in the first two list, 159 00:08:42,150 --> 00:08:46,560 ‫then I can get Lars and the age of the Lars with 60, 160 00:08:46,560 --> 00:08:50,640 ‫for example, this is a good example of leveraging arrays. 161 00:08:50,640 --> 00:08:53,640 ‫For example, if I had two different sources, 162 00:08:53,640 --> 00:08:57,300 ‫from internet to download, that I could have downloaded them 163 00:08:57,300 --> 00:08:59,640 ‫and just edit them in order, 164 00:08:59,640 --> 00:09:03,150 ‫so that they will coincide with the indexes, 165 00:09:03,150 --> 00:09:05,970 ‫so I can get the first index, second index, 166 00:09:05,970 --> 00:09:07,770 ‫in order to find the name 167 00:09:07,770 --> 00:09:12,243 ‫and the age of given elements, okay? 168 00:09:13,290 --> 00:09:15,750 ‫For example I could have done this as well, 169 00:09:15,750 --> 00:09:18,090 ‫int my number array, okay? 170 00:09:18,090 --> 00:09:19,920 ‫Rather than saying three, 171 00:09:19,920 --> 00:09:21,750 ‫rather than giving out the capacity, 172 00:09:21,750 --> 00:09:24,900 ‫I can just open curly braces over here 173 00:09:24,900 --> 00:09:28,050 ‫and write the numbers like this. 174 00:09:28,050 --> 00:09:30,720 ‫1, 2, 3, 4, 5, 7, 8. 175 00:09:30,720 --> 00:09:32,460 ‫Now I have the same thing. 176 00:09:32,460 --> 00:09:36,360 ‫I can reach the same thing over here. 177 00:09:36,360 --> 00:09:39,630 ‫For example, if I come over here and say two, 178 00:09:39,630 --> 00:09:43,713 ‫what will I get over in the logs? 179 00:09:45,180 --> 00:09:50,180 ‫So if you had said three, then you are completely right. 180 00:09:50,760 --> 00:09:51,900 ‫Why do we get three? 181 00:09:51,900 --> 00:09:56,220 ‫Because it's zero, okay, here and one over here 182 00:09:56,220 --> 00:09:58,320 ‫and two over here. 183 00:09:58,320 --> 00:10:01,380 ‫Number three has the index of two, 184 00:10:01,380 --> 00:10:06,380 ‫because the index starts with zero in the arrays. 185 00:10:06,390 --> 00:10:11,160 ‫So this is one of the methods that you can define an array, 186 00:10:11,160 --> 00:10:13,530 ‫without giving the capacity first. 187 00:10:13,530 --> 00:10:16,440 ‫For example, over here, if I do this, 188 00:10:16,440 --> 00:10:21,180 ‫my integer array three 80, okay, it would work fine. 189 00:10:21,180 --> 00:10:25,200 ‫But if I run this, then I will get an error, 190 00:10:25,200 --> 00:10:27,450 ‫because as you can see, 191 00:10:27,450 --> 00:10:31,830 ‫I get this array index out of bounds exception, 192 00:10:31,830 --> 00:10:33,660 ‫it's actually out of bonds, 193 00:10:33,660 --> 00:10:37,050 ‫because I have a restriction over here, 194 00:10:37,050 --> 00:10:39,780 ‫I specified the capacity as three. 195 00:10:39,780 --> 00:10:43,380 ‫Now, I should put three elements inside of this array. 196 00:10:43,380 --> 00:10:45,180 ‫If I put the fourth one. 197 00:10:45,180 --> 00:10:46,013 ‫As you can see, 198 00:10:46,013 --> 00:10:50,373 ‫it gives me array index out of bounds exception. 199 00:10:51,270 --> 00:10:55,590 ‫So if we change this to be four, I can just come over here 200 00:10:55,590 --> 00:10:58,920 ‫and add the fourth element as well like this 201 00:10:58,920 --> 00:11:02,820 ‫and I won't get any errors after all. 202 00:11:02,820 --> 00:11:05,370 ‫So this was an introduction to arrays. 203 00:11:05,370 --> 00:11:08,180 ‫We will continue learning about new kind of arrays, 204 00:11:08,180 --> 00:11:09,933 ‫in the next lecture.