1 00:00:00,840 --> 00:00:06,450 Welcome back in this video, we're going to look at the race and race, as we saw earlier, allow us 2 00:00:06,450 --> 00:00:11,040 to store multiple pieces of data in one variable, so to speak. 3 00:00:11,340 --> 00:00:18,450 So let's create our first rate for the integer type and also see the different ways we can create an 4 00:00:18,450 --> 00:00:18,750 array. 5 00:00:19,200 --> 00:00:25,900 So first of all, I'm going to use the keyword volume and I'm going to call this variable numbers and 6 00:00:25,900 --> 00:00:27,420 that will be of type entry. 7 00:00:27,660 --> 00:00:35,010 OK, so using in-tray and then I need to use this method called int array of where I can then assign 8 00:00:35,010 --> 00:00:35,560 values. 9 00:00:35,640 --> 00:00:43,350 So int values and multiple ones so far are elements of Typekit, which means I can just go ahead and 10 00:00:43,740 --> 00:00:47,640 just add a couple of numbers separated with a comma. 11 00:00:48,000 --> 00:00:50,430 So I have these values here one, two six. 12 00:00:50,730 --> 00:00:52,470 So that's one way of creating it. 13 00:00:52,920 --> 00:01:00,360 But then because of type inference, we can just go ahead and use a simpler approach where we can get 14 00:01:00,360 --> 00:01:03,180 rid of this in-tray here entirely. 15 00:01:03,180 --> 00:01:04,349 So let me copy that. 16 00:01:04,709 --> 00:01:06,630 And here we can get rid of this part. 17 00:01:06,630 --> 00:01:09,060 Therefore, I need to comment this out. 18 00:01:09,360 --> 00:01:15,090 So because we are using int array of numbers knows that it should be an array of integers. 19 00:01:15,240 --> 00:01:16,560 So that's type inference here. 20 00:01:16,560 --> 00:01:21,300 We don't need to specifically state that it's going to be off type into array. 21 00:01:22,080 --> 00:01:26,150 So by the way, there is an even simpler way of doing it. 22 00:01:26,220 --> 00:01:33,840 So another way of using type inference, and I'm going to again copy the line of code comment the other 23 00:01:33,840 --> 00:01:34,470 way out. 24 00:01:34,500 --> 00:01:36,240 And by the way, all of them are valid, right? 25 00:01:36,570 --> 00:01:38,160 These are just more descriptive. 26 00:01:38,160 --> 00:01:42,660 So they are really, say, throwing to the point what's going on here? 27 00:01:43,260 --> 00:01:50,070 And then we have this one where we don't even need to say that it's an entry, which is to say that 28 00:01:50,070 --> 00:01:51,480 it's an array of. 29 00:01:51,960 --> 00:01:54,120 So now let's go ahead and print an array. 30 00:01:54,360 --> 00:01:59,250 We can just go ahead and say print numbers, and let's run this real quick. 31 00:01:59,580 --> 00:02:02,940 Then we will see that it just prints this information for us. 32 00:02:03,120 --> 00:02:06,300 So we would have expected to get the numbers here, right? 33 00:02:06,930 --> 00:02:09,900 But what we're getting is actually this year. 34 00:02:10,470 --> 00:02:16,590 So we are getting the address of the array, which is where it is stored in memory. 35 00:02:17,160 --> 00:02:20,130 This is because we cannot just print a race with the print method. 36 00:02:20,340 --> 00:02:25,230 We will need to use a utility method provided in Scotland for that purpose. 37 00:02:25,830 --> 00:02:31,470 So what we can do is we can use numbers and that's a cool thing of the array off here. 38 00:02:32,070 --> 00:02:37,920 So this is an array now and we can just go ahead and use a method called content. 39 00:02:39,760 --> 00:02:40,930 To string. 40 00:02:42,460 --> 00:02:48,940 So what that will do is it will transfer the content into a string, convert it for us, and then we 41 00:02:48,940 --> 00:02:54,160 can now display the numbers and you can see now it just says, OK, this is an array, and that's why 42 00:02:54,160 --> 00:02:55,570 we have those square brackets here. 43 00:02:55,600 --> 00:02:57,070 One two three four five six. 44 00:02:58,210 --> 00:03:01,750 OK, so now we can see the values in the right format. 45 00:03:02,290 --> 00:03:09,880 So now let's traverse through in a way, meaning go through the items of the array and print them individually. 46 00:03:10,570 --> 00:03:13,180 So that would be a different approach. 47 00:03:13,270 --> 00:03:13,990 Let's look at it. 48 00:03:15,430 --> 00:03:21,820 And I'm going to simplify the code here, and the easiest way to do it is to use a for loop where I'm 49 00:03:21,820 --> 00:03:26,590 just going to say an element inside of numbers. 50 00:03:27,550 --> 00:03:29,680 And this element is just a variable name. 51 00:03:29,680 --> 00:03:32,470 You could have called it I or whatever you want it. 52 00:03:32,710 --> 00:03:37,330 And I'm just going to say the element in numbers is what I want to print. 53 00:03:37,480 --> 00:03:39,640 So let's print elements here. 54 00:03:40,300 --> 00:03:44,470 So what's happening now is that it goes through these numbers array. 55 00:03:44,710 --> 00:03:50,170 And for every single item starting from the 1st, it will store that in element. 56 00:03:50,410 --> 00:03:54,970 In the first iteration and the second iteration, it will store the two in element. 57 00:03:54,970 --> 00:03:57,940 In the third iteration, it will store to three an element and so forth. 58 00:03:58,240 --> 00:04:03,220 So it's going to go through all the numbers inside of this numbers array and we'll store them in element 59 00:04:03,220 --> 00:04:07,270 and then print them out and then go to the next number printed out, go to the next number and so forth. 60 00:04:07,660 --> 00:04:12,400 So here we're just printing, but you could do whatever you want to do with that particular element. 61 00:04:13,030 --> 00:04:15,420 You could modify it and store it somewhere else. 62 00:04:15,430 --> 00:04:17,829 You could display it and so forth. 63 00:04:18,279 --> 00:04:23,080 So we're just going to display on the console using a print state and you can see one, two, three, 64 00:04:23,080 --> 00:04:24,130 four, five and six. 65 00:04:24,220 --> 00:04:27,130 So all the numbers that are inside of numbers are now being displayed. 66 00:04:28,730 --> 00:04:34,940 So now let's actually do a modification here, so I'm just going to go ahead and add an empty space 67 00:04:34,940 --> 00:04:37,640 here and add two to it. 68 00:04:37,790 --> 00:04:39,470 So here plus two, for example. 69 00:04:43,020 --> 00:04:48,470 Now, if I run that, I will see that it will just display one plus two, two plus two and so forth, 70 00:04:48,480 --> 00:04:50,310 it's not doing the actual calculation. 71 00:04:50,640 --> 00:04:58,230 Do you recall how to actually do the calculation here when we were using string manipulation here? 72 00:04:58,740 --> 00:05:01,080 What we need to add the curly brackets around it. 73 00:05:01,470 --> 00:05:02,730 So let's run it again. 74 00:05:03,300 --> 00:05:06,570 And now it will add the number to the element. 75 00:05:08,650 --> 00:05:12,190 But that by itself will not change the numbers itself. 76 00:05:12,530 --> 00:05:20,110 OK, so if we were to now do the same thing once again after that and we print out numbers without adding 77 00:05:20,110 --> 00:05:24,970 the two to it, therefore we don't need to have those brackets. 78 00:05:24,970 --> 00:05:30,490 Let's run this code and you will see that our numbers are not actually impacted. 79 00:05:30,640 --> 00:05:32,980 So three four five six seven eight. 80 00:05:33,310 --> 00:05:35,710 But the actual numbers are still one two three four five. 81 00:05:35,720 --> 00:05:38,400 So we didn't change the value itself. 82 00:05:38,410 --> 00:05:42,370 We just displayed it, so we didn't overwrite it. 83 00:05:45,270 --> 00:05:53,070 So now let's modify a value in the array, and since arrays are indexed based on starts counting from 84 00:05:53,070 --> 00:05:57,060 zero, it means a position of one in our array is going to be zero. 85 00:05:57,210 --> 00:06:01,590 The index of two or the position of two will be one and so forth. 86 00:06:01,590 --> 00:06:02,640 It's a little confusing. 87 00:06:02,850 --> 00:06:05,940 But let's look at it in order to understand it a little better. 88 00:06:06,300 --> 00:06:08,550 So I'm going to get rid of this for loop here. 89 00:06:08,940 --> 00:06:16,280 I'm going to print numbers and now if I want to access a particular item inside of the numbers array, 90 00:06:16,290 --> 00:06:18,960 I just need to use the square brackets here. 91 00:06:19,260 --> 00:06:24,210 So if I, for example, want to access index zero, this will give me one. 92 00:06:24,330 --> 00:06:25,110 Let's test this. 93 00:06:27,160 --> 00:06:32,650 And you will see that at this place one, in fact, because it starts at index zero, but the first 94 00:06:32,650 --> 00:06:34,900 entry that we have is one at this position. 95 00:06:35,140 --> 00:06:39,040 So now let's use Index three, for example, and that will give us the four. 96 00:06:39,850 --> 00:06:40,630 Let's run it again. 97 00:06:42,610 --> 00:06:44,290 And here you see four. 98 00:06:44,320 --> 00:06:47,430 So this has nothing to do with the actual numbers that are in here. 99 00:06:47,440 --> 00:06:50,100 It just has to do with the position that we are looking at. 100 00:06:50,110 --> 00:06:52,360 So the index of that array. 101 00:06:52,600 --> 00:06:57,850 So here, let's say I'm entering five and I get the number at position zero. 102 00:06:58,090 --> 00:07:02,950 Now it will give me the five instead of the one because that's what is stored at position zero. 103 00:07:03,730 --> 00:07:05,530 OK, let me go back to one here. 104 00:07:08,270 --> 00:07:11,690 So now let's modify our arrays a little bit. 105 00:07:12,050 --> 00:07:14,220 So now let's modify the array a little bit. 106 00:07:14,300 --> 00:07:19,800 I want to store a different value at position zero, for example, in order to achieve that. 107 00:07:19,820 --> 00:07:27,980 I just use the name of the variable, which is numbers then are used to square brackets with the index, 108 00:07:27,980 --> 00:07:31,700 which I want to change, and then I assign the value that I want to assign. 109 00:07:32,090 --> 00:07:34,310 So in this case, let's say it's position zero. 110 00:07:34,310 --> 00:07:44,510 I want to use the number six and at position one, I want to use the number five and then four index. 111 00:07:45,800 --> 00:07:52,010 I'm going to use the number two and finally four index five. 112 00:07:53,380 --> 00:07:55,510 I'm going to use the one. 113 00:07:56,500 --> 00:08:05,830 OK, so let's now look at what happened, so first of all, I'm going to print the initial value of 114 00:08:05,830 --> 00:08:09,400 my numbers and I'm going to call this one initial. 115 00:08:11,260 --> 00:08:11,950 Values. 116 00:08:13,840 --> 00:08:18,280 And then used to square or the dollar sign with. 117 00:08:20,340 --> 00:08:26,970 The round, the bracket surrounding it, like so and then I'm going to print it with that. 118 00:08:27,980 --> 00:08:29,300 Changed values, so. 119 00:08:30,940 --> 00:08:36,549 With a new line, by the way, therefore, I'm using the backslash end and then going to call it final 120 00:08:36,549 --> 00:08:37,030 values. 121 00:08:37,659 --> 00:08:42,190 OK, so let's run this and see how this has impacted our array here. 122 00:08:43,860 --> 00:08:48,090 We could see initial values were one two three four five six, and the final values were six five three 123 00:08:48,090 --> 00:08:48,810 four two one. 124 00:08:51,590 --> 00:08:58,010 So now, since arrays have a fixed size, we cannot increase the size or try to add a new index since 125 00:08:58,010 --> 00:09:01,130 we have six values and the index starts from zero to five. 126 00:09:01,460 --> 00:09:09,410 If we try to add something to numbers at the index seven, for example, this would not work so I can 127 00:09:09,410 --> 00:09:12,350 get into a bounce out of balance exception doing that. 128 00:09:12,410 --> 00:09:15,050 So let's say I want to assign two to position seven. 129 00:09:15,770 --> 00:09:22,250 Let's run this and you see we get an error here, it says exception in thread main array index out of 130 00:09:22,250 --> 00:09:23,120 bounds exception. 131 00:09:23,450 --> 00:09:26,090 So Index seven out of bounds for length six. 132 00:09:26,390 --> 00:09:29,510 So we try to access an index that doesn't exist. 133 00:09:29,510 --> 00:09:34,160 So the same would even go for Index six because we only have six entries and it starts at zero and goes 134 00:09:34,160 --> 00:09:34,940 all the way to five. 135 00:09:34,970 --> 00:09:36,140 So here exception. 136 00:09:36,140 --> 00:09:38,180 So that main index six out of bounds for length. 137 00:09:38,180 --> 00:09:38,530 Six. 138 00:09:39,470 --> 00:09:40,780 Okay, because it starts at zero. 139 00:09:40,790 --> 00:09:42,920 That's why six is already out of bounds. 140 00:09:44,060 --> 00:09:46,520 Now we can use the same with the double array. 141 00:09:46,940 --> 00:09:53,440 So numbers, for example, this would be our array just using the double array keyword off. 142 00:09:53,750 --> 00:09:56,900 And of course, you could have simplified this the same way. 143 00:09:57,230 --> 00:10:00,800 And here I'm going to override the double array. 144 00:10:00,980 --> 00:10:08,330 Like so but then you have to specifically use double numbers, which means you have to add the floating 145 00:10:08,330 --> 00:10:09,440 point value to them. 146 00:10:09,440 --> 00:10:10,780 Otherwise, it wouldn't work. 147 00:10:10,790 --> 00:10:12,970 So you cannot assign an integer to a double array. 148 00:10:12,980 --> 00:10:15,630 You couldn't see that directly here because we got an error here. 149 00:10:15,650 --> 00:10:16,160 Otherwise. 150 00:10:17,470 --> 00:10:21,670 Or you can go ahead and use an array of four days. 151 00:10:21,850 --> 00:10:24,370 So let's go ahead and use that here. 152 00:10:24,790 --> 00:10:28,210 So while days will be an array of. 153 00:10:28,720 --> 00:10:31,640 And you just use Sun, for example. 154 00:10:32,050 --> 00:10:38,560 Then Monday and you see you separate the entries even if you're using strings each time with a comma. 155 00:10:39,130 --> 00:10:43,090 So each entry is handed like this inside. 156 00:10:44,120 --> 00:10:45,530 Of our brackets. 157 00:10:46,250 --> 00:10:51,590 So then if you want to display it, you would do the same thing as we saw earlier with content string. 158 00:10:52,190 --> 00:10:56,630 So here I have all of the days of the week and now I'm printing them and. 159 00:10:58,600 --> 00:11:01,550 This will have not any impact here. 160 00:11:01,570 --> 00:11:07,300 Let's also get rid of this statement here, and let's just display the days of the week and you see 161 00:11:07,300 --> 00:11:09,520 Sunday, Monday, Tuesday, Wednesday and so forth. 162 00:11:10,840 --> 00:11:15,580 We can even go as far as to store entire objects inside of an array. 163 00:11:15,730 --> 00:11:20,470 So therefore, I'm going to create a small data class outside of this function, also outside of our 164 00:11:20,470 --> 00:11:24,550 main function here at the bottom, and I'm going to call this one fruit. 165 00:11:26,180 --> 00:11:28,190 And this fruit will need a name. 166 00:11:28,460 --> 00:11:33,740 So while name of type string and it will need a price, for example, of type double. 167 00:11:34,880 --> 00:11:37,880 OK, so that's how the structure of our data class will be. 168 00:11:38,090 --> 00:11:46,220 And now we can just go ahead and create a fruits list, which will be an array of. 169 00:11:47,920 --> 00:11:48,890 The different fruits. 170 00:11:48,910 --> 00:11:56,230 So here we can now create fruit objects of fruit object one will be an apple and it has a price of, 171 00:11:56,740 --> 00:12:02,680 let's say, a price of two point five dollars or euros and then create another fruit. 172 00:12:04,130 --> 00:12:10,610 Which will be, for example, great and that will then cost three point five euros per kilo just as 173 00:12:10,610 --> 00:12:11,210 an example. 174 00:12:11,690 --> 00:12:17,480 And we can print the fruits in the same way as we would with the days. 175 00:12:17,510 --> 00:12:23,000 So here this is print fruits with content to string, and let's run it real quick. 176 00:12:24,880 --> 00:12:32,020 And we can see here we have the fruit named Apple Price two point five and the other fruit grape and 177 00:12:32,020 --> 00:12:32,410 so forth. 178 00:12:32,710 --> 00:12:35,930 This will be super useful and we're going to use that quite a bit in the future. 179 00:12:37,800 --> 00:12:41,340 Now you can also access individual items. 180 00:12:42,760 --> 00:12:49,140 Of that fruit and printed instead of printing all the fruits content, you can just go ahead and just 181 00:12:49,860 --> 00:12:51,540 display the fruits name. 182 00:12:52,080 --> 00:12:57,420 OK, so I could just go ahead and say where the fruit is at which index. 183 00:12:57,840 --> 00:13:02,760 So here let me use the quotation marks a rounding. 184 00:13:03,120 --> 00:13:05,130 Basically, I'm saying fruits. 185 00:13:05,430 --> 00:13:06,720 Give me the index as well. 186 00:13:06,720 --> 00:13:08,940 And in order to get the index, we of course need to. 187 00:13:10,100 --> 00:13:18,740 There's a for loop, so for index in roots and then indices. 188 00:13:20,500 --> 00:13:23,590 This gives me the indexes like so. 189 00:13:24,890 --> 00:13:29,960 Then inside I would just to print index, so this doesn't give me the fruit itself, but it gives me 190 00:13:29,960 --> 00:13:32,060 the index of the position of the fruit. 191 00:13:32,540 --> 00:13:37,160 So here I want to get the name of the fruit at a particular index. 192 00:13:37,430 --> 00:13:38,360 That would be a weight. 193 00:13:38,870 --> 00:13:39,380 And. 194 00:13:40,520 --> 00:13:43,310 Let me add the quotation here as well. 195 00:13:43,760 --> 00:13:48,260 This is one way to get the indexes, but if you just want to get the fruits, there is an easier way. 196 00:13:48,500 --> 00:13:56,630 You can just use a follow-up where you go for each individual fruit inside of fruits and you just print 197 00:13:56,840 --> 00:13:58,340 the fruit name. 198 00:13:58,610 --> 00:14:03,230 So here you can just go ahead and say fruit dot name. 199 00:14:03,720 --> 00:14:05,240 And this will give you the fruit name. 200 00:14:05,990 --> 00:14:06,890 So let's run it. 201 00:14:08,120 --> 00:14:11,870 And you will see that we now get the fruit names. 202 00:14:12,260 --> 00:14:17,060 And let me actually get rid of the base here so we can see a little better. 203 00:14:18,700 --> 00:14:22,730 And we could see Apple and grape, and then Apple is at index zero. 204 00:14:22,750 --> 00:14:24,460 Rape is index one. 205 00:14:26,560 --> 00:14:29,980 So this was apple and grape, and this was the indices. 206 00:14:31,600 --> 00:14:39,040 By the way, you can also have an array which uses multiple different types of data, so here let's 207 00:14:39,040 --> 00:14:45,820 use this days, for example, and instead of days now we have this little mix up where we use Tuesday 208 00:14:45,820 --> 00:14:51,370 and then we just assign a numbers like, so it's just a new number. 209 00:14:51,370 --> 00:14:59,200 And then here we also have a fruit object so we can just go ahead and say fruit apple with a two point 210 00:14:59,200 --> 00:15:00,160 five price. 211 00:15:00,700 --> 00:15:04,140 So this would be in a taste, but mix and then this print those as well. 212 00:15:04,150 --> 00:15:07,090 So here mix that content to string. 213 00:15:08,000 --> 00:15:13,160 And therefore, let me comment those other parts out, and let's run our quote. 214 00:15:15,850 --> 00:15:21,130 So you see, we have Sunday, Monday, Tuesday, one to three fruit with the name Apple and price of 215 00:15:21,130 --> 00:15:21,820 two point five. 216 00:15:22,120 --> 00:15:26,830 So the beauty about resolve is really that it can hold different types of data. 217 00:15:27,100 --> 00:15:32,530 And we saw earlier that there are these double arrays, for example, they can only hold doubles and 218 00:15:32,530 --> 00:15:33,040 interest. 219 00:15:33,040 --> 00:15:34,180 It can only hold ends. 220 00:15:34,420 --> 00:15:37,120 There will be more efficient as well as safer. 221 00:15:37,330 --> 00:15:39,160 So why would you use an interface? 222 00:15:39,160 --> 00:15:40,930 You can just go ahead and use an array of? 223 00:15:41,170 --> 00:15:46,370 Well, the idea is that interface are super on point and they can only hold numbers. 224 00:15:46,390 --> 00:15:48,310 So imagine you get the data from somewhere else. 225 00:15:48,310 --> 00:15:53,500 And for some reason, the database gave you an integer the position where you would have expected an 226 00:15:53,500 --> 00:16:00,090 end and then your coat would directly figure it out and you could get away around it. 227 00:16:00,220 --> 00:16:05,800 Except you could add an exception that would then manage that problem and go about it. 228 00:16:06,130 --> 00:16:11,890 OK, and that wouldn't be recognized if you have such a mixed array that doesn't care what kind of data 229 00:16:11,890 --> 00:16:12,370 it has. 230 00:16:13,720 --> 00:16:16,000 OK, so that's pretty much it for this video. 231 00:16:16,480 --> 00:16:17,440 See you in the next one.