1 00:00:02,760 --> 00:00:08,520 And the structure I would like to talk about race, and first I would like to start with a problem that 2 00:00:08,520 --> 00:00:09,920 can be solved with them. 3 00:00:10,530 --> 00:00:17,280 So let's just say we have a car, we travel to another country, and we would like to calculate the 4 00:00:17,280 --> 00:00:18,890 average fuel consumption. 5 00:00:19,650 --> 00:00:24,330 We have the data for every 100 kilometers or miles. 6 00:00:25,260 --> 00:00:33,420 If the destination is close, for example, 200 kilometers away, we can solve this by using two variables. 7 00:00:34,050 --> 00:00:36,570 You add them up and divide them by two. 8 00:00:36,900 --> 00:00:37,430 Easy. 9 00:00:38,190 --> 00:00:42,720 Let's just say the destination is 500 kilometers away. 10 00:00:43,350 --> 00:00:44,910 OK, five variables. 11 00:00:45,090 --> 00:00:52,320 We are still good in what you're going to do when you have to travel 5000 kilometres, you should define 12 00:00:52,320 --> 00:00:55,070 50 variables to store that data. 13 00:00:55,350 --> 00:00:57,660 That's when a race can be used. 14 00:00:58,140 --> 00:00:59,850 They are like containers. 15 00:01:00,090 --> 00:01:02,790 You can store a lot of data in them. 16 00:01:03,660 --> 00:01:11,550 OK, so what arrays are by definition, they are used to store multiple values with the same data type 17 00:01:11,550 --> 00:01:13,290 in a single variable. 18 00:01:13,920 --> 00:01:21,570 That data type can be anything a primitive, a built in object or a custom object that you created. 19 00:01:22,170 --> 00:01:29,730 They can be numbers like fuel consumption, lottery numbers or text like features of a car or movie 20 00:01:29,730 --> 00:01:30,540 titles. 21 00:01:30,870 --> 00:01:38,880 But as I said, they store objects like a lot of cars or movies with all the information that belongs 22 00:01:38,880 --> 00:01:39,270 to them. 23 00:01:40,580 --> 00:01:43,550 Now, let's see what we are going to cover in this lecture. 24 00:01:43,940 --> 00:01:51,500 First, I will show you how to create an array, then what default values are assigned to the elements 25 00:01:51,500 --> 00:01:53,480 in an array after creation. 26 00:01:53,810 --> 00:02:01,970 Right after that, I will explain array indexing how to use it to get the data after that initialization, 27 00:02:02,510 --> 00:02:04,700 then loops for arrays. 28 00:02:05,270 --> 00:02:09,650 And finally, science fiction, the multidimensional arrays. 29 00:02:10,130 --> 00:02:13,100 And as usually there is going to be a coding party. 30 00:02:14,430 --> 00:02:20,790 One last thing I would like to point out here is that we are not going to go very deep in a race and 31 00:02:20,790 --> 00:02:24,870 this lecture, there will be an advanced level race video for that. 32 00:02:26,340 --> 00:02:27,740 All right, let's do this. 33 00:02:29,020 --> 00:02:35,620 We're going to use lottery numbers in this lecture as an example, in my country, we have many lottery 34 00:02:35,620 --> 00:02:36,190 types. 35 00:02:36,550 --> 00:02:45,040 The most popular one is when there are 90 numbers from one to 90 and you have to choose five and you 36 00:02:45,040 --> 00:02:48,530 have to put crosses on them, as you can see on the picture. 37 00:02:49,330 --> 00:02:55,420 So how you can create an array that can store the lottery numbers first you have to define the data 38 00:02:55,420 --> 00:03:00,070 type that will be stored, then square brackets that shows Java. 39 00:03:00,280 --> 00:03:02,010 That's not a variable. 40 00:03:02,770 --> 00:03:12,190 After that, the arrays name lottery numbers then equals new keywords, the data type again and the 41 00:03:12,190 --> 00:03:15,000 size of the array in square brackets. 42 00:03:15,490 --> 00:03:17,310 And finally, a semicolon. 43 00:03:18,100 --> 00:03:24,220 You can also separate the creation and the definition of the size, but you cannot use it until you 44 00:03:24,220 --> 00:03:27,640 don't tell Java was the certain size of the array. 45 00:03:28,970 --> 00:03:35,570 The problem with race is that you cannot increase or decrease their size if you know the certain size 46 00:03:35,720 --> 00:03:36,620 you will need. 47 00:03:36,860 --> 00:03:37,970 They are a very good. 48 00:03:39,210 --> 00:03:45,780 Later in the course, I will show you what you can use instead of array's, but now move on to default 49 00:03:45,780 --> 00:03:46,380 values. 50 00:03:47,010 --> 00:03:52,110 After specifying the size of the array, the array is filled up with default values. 51 00:03:52,260 --> 00:03:54,940 In case of integer, it is going to be zero. 52 00:03:55,440 --> 00:03:57,420 It is the same for other hole. 53 00:03:57,420 --> 00:04:00,930 No data types like both short and long. 54 00:04:01,230 --> 00:04:07,140 In case of float and double, it is going to be zero point zero for boolean. 55 00:04:07,140 --> 00:04:12,080 It is false for Char has a special character, the normal character. 56 00:04:12,630 --> 00:04:18,360 You can see the character code on the right and for object types it is pretty easy. 57 00:04:18,570 --> 00:04:23,520 All object data types default value is no, which means empty object. 58 00:04:24,180 --> 00:04:30,240 Be careful not for objects and no character for char are not the same. 59 00:04:31,770 --> 00:04:39,900 Next is the next thing, so we have an array full of zeros and index is used to select one element of 60 00:04:39,900 --> 00:04:40,510 an array. 61 00:04:41,010 --> 00:04:49,650 The thing starts from zero, not one, like in the case of lottery numbers, zero is the first index 62 00:04:49,650 --> 00:04:53,910 and the last is for OK, how to use indexes. 63 00:04:54,210 --> 00:04:58,890 Let's just say we would like to assign 17 to the first element to do that. 64 00:04:58,900 --> 00:05:07,050 First, the erased name, lottery number, the index of the element we would like to use in square brackets 65 00:05:07,570 --> 00:05:12,630 because then the value 17 and the semicolon. 66 00:05:13,230 --> 00:05:22,320 Now the second one, lottery numbers, one in square brackets equals eighty two and a semicolon to use 67 00:05:22,320 --> 00:05:24,040 the elements of the array. 68 00:05:24,060 --> 00:05:25,680 The formula is almost the same. 69 00:05:26,220 --> 00:05:33,960 So to print out one element with system that out that printer and use the arrays name and the index 70 00:05:33,960 --> 00:05:40,080 in square brackets, you can also assign an array element to another variable. 71 00:05:40,290 --> 00:05:41,280 Just like this. 72 00:05:41,790 --> 00:05:46,490 New numbers value is going to be seventeen plus one eighteen. 73 00:05:47,460 --> 00:05:49,380 You can also override an array. 74 00:05:49,380 --> 00:05:54,600 Elements value the same way as you first assign value to it. 75 00:05:57,310 --> 00:06:03,100 Now, let's continue with initialization, when you initialize, you don't have to tell Java what's 76 00:06:03,100 --> 00:06:08,920 the size of the array because you define the elements in curly braces. 77 00:06:09,860 --> 00:06:12,770 That will make Java define the size. 78 00:06:13,760 --> 00:06:20,810 It can be used when you know the values right at the beginning, and it is very handy because you don't 79 00:06:20,810 --> 00:06:28,520 have to bother with the indexes when you assign values, but there are many cases when you have to calculate 80 00:06:28,520 --> 00:06:30,110 elements later in the code. 81 00:06:31,360 --> 00:06:38,440 Now move on to Loop's for a raise, the easiest way to go through every element of an array is to use 82 00:06:38,440 --> 00:06:41,770 a loop for loop is one of the best. 83 00:06:42,040 --> 00:06:47,920 You start with assigning zero to I, then in the condition I. 84 00:06:48,610 --> 00:06:52,750 It's very important less than the lottery numbers. 85 00:06:52,930 --> 00:06:58,910 And here you can use that length to get the size or length of the array. 86 00:06:59,290 --> 00:07:07,810 And last I plus plus as usually and you can use I as an index, just as you can see in the print and 87 00:07:07,840 --> 00:07:12,550 function, the Falu prints out every element of the array. 88 00:07:13,510 --> 00:07:18,340 There is another type of loop I didn't talk about in the loops lecture. 89 00:07:18,680 --> 00:07:20,230 It is called for each. 90 00:07:20,800 --> 00:07:28,690 It starts with the four key word, but in the parentheses, first you define the data type of the elements 91 00:07:28,900 --> 00:07:37,780 in the array, then the name you would like to refer to in the code, then column and finally the source, 92 00:07:37,780 --> 00:07:40,750 the arrays name in the foreach loop. 93 00:07:40,750 --> 00:07:44,770 You can use the lottery number for one element of the array. 94 00:07:45,040 --> 00:07:52,960 You don't have to bother with indexing or length less than or starting with zero for each is good one. 95 00:07:52,960 --> 00:07:55,930 You don't need to use the index for any other purposes. 96 00:07:56,500 --> 00:07:59,080 You just simply run through the whole array. 97 00:08:00,310 --> 00:08:06,880 Moving on to our last topic in this lecture, the multidimensional race, in case of lottery numbers, 98 00:08:06,880 --> 00:08:11,890 we can store numbers for a week and a simple one dimensional array. 99 00:08:12,310 --> 00:08:14,220 But there are many weeks in a year. 100 00:08:14,410 --> 00:08:18,280 It's around 50 for our array for a year. 101 00:08:18,400 --> 00:08:21,630 Filled up with numbers would look like this. 102 00:08:22,030 --> 00:08:26,210 The indexing starts from zero for both columns and rows. 103 00:08:26,680 --> 00:08:30,690 So how to create such an array for multidimensional arrays? 104 00:08:30,700 --> 00:08:34,390 You have to use two square brackets instead of one. 105 00:08:34,690 --> 00:08:41,440 After the data type, then the arrays name equals then the data type again. 106 00:08:41,680 --> 00:08:47,140 And where you have to tell you about the size of the array, you have to specify both sizes. 107 00:08:47,660 --> 00:08:50,560 The indexing starts from zero, as I said. 108 00:08:50,740 --> 00:08:57,160 So the first element is weekly lottery numbers, square brackets zero, then square brackets zero again 109 00:08:57,790 --> 00:09:00,240 and its value is seventeen. 110 00:09:00,760 --> 00:09:09,040 The last element is weekly lottery numbers, square brackets 53, then four in square brackets and last 111 00:09:09,040 --> 00:09:11,950 multidimensional arrays can be initialized to. 112 00:09:12,280 --> 00:09:18,090 To do that, you have to use different level of curly braces separated by comma. 113 00:09:18,970 --> 00:09:26,410 Be careful with this because it is very easy to miss a comma or put a curly brace to the wrong place. 114 00:09:28,000 --> 00:09:29,240 That's it for now. 115 00:09:29,410 --> 00:09:32,470 We will talk more about race later in the course. 116 00:09:32,620 --> 00:09:35,530 I hope it was clear see in the cording part.