1 00:00:00,180 --> 00:00:05,580 Arrays and loops are like siblings previously defined an array that stores four kingdoms and you learn 2 00:00:05,580 --> 00:00:07,710 how to access each element using its index. 3 00:00:13,780 --> 00:00:18,340 Now, let's imagine the array had 20 elements, are you going to put 20 print statements and access 4 00:00:18,340 --> 00:00:19,750 each index one by one? 5 00:00:20,140 --> 00:00:21,270 Absolutely not. 6 00:00:21,280 --> 00:00:22,180 That would be terrible. 7 00:00:22,870 --> 00:00:26,070 Instead, we can use a loop that runs through every element in the array. 8 00:00:26,620 --> 00:00:29,740 In this video, you're going to learn how to loop through an array of values. 9 00:00:33,810 --> 00:00:38,160 The first thing you need to do is create a new class by yourself inside Section six. 10 00:00:38,670 --> 00:00:43,860 Wow, that's a tongue twister inside your Section six folder, create a new file, need to raise an 11 00:00:43,860 --> 00:00:46,710 Leapster Java and make sure the class has a main method. 12 00:00:53,480 --> 00:00:58,280 So how can we loop through an array, a loop can run through every element in an array, but how does 13 00:00:58,280 --> 00:00:58,880 that happen? 14 00:00:59,270 --> 00:01:00,080 Let's have a look. 15 00:01:02,500 --> 00:01:06,640 Inside arrays and loops make another kingdom's variable, it's going to be the exact same variable as 16 00:01:06,640 --> 00:01:11,140 the last one, string kingdom's is equal to an array of string values. 17 00:01:16,110 --> 00:01:16,890 W6. 18 00:01:18,470 --> 00:01:20,540 Northumbria and East Anglia. 19 00:01:24,990 --> 00:01:26,010 Yay for short. 20 00:01:27,440 --> 00:01:32,510 The array has four elements, so you need a for loop that runs exactly four times, so I'm going to 21 00:01:32,510 --> 00:01:34,490 say four enties is equal to zero. 22 00:01:36,110 --> 00:01:41,180 The eye counter is going to start at zero because the index of a race first element starts at zero as 23 00:01:41,180 --> 00:01:41,450 well. 24 00:01:42,080 --> 00:01:45,900 And so it makes sense because the last index of an array race three. 25 00:01:46,280 --> 00:01:51,950 The loop is going to keep running while the counter eye is less than the array's length for E-Plus plus. 26 00:01:58,540 --> 00:02:01,180 And then I'm going to print the counter I during each run. 27 00:02:18,770 --> 00:02:24,620 All right, this loop keeps running as long as it is less than four, I starts at zero and goes up until 28 00:02:24,620 --> 00:02:30,440 three, basically designed this loop so that each counter value matches the index of an element so I 29 00:02:30,440 --> 00:02:34,250 can use each counter value to index an element inside the kingdom array. 30 00:02:35,930 --> 00:02:38,690 Some print the element at Inducts I. 31 00:02:51,830 --> 00:02:54,620 And here will index the element from the kingdom's array. 32 00:03:07,430 --> 00:03:10,760 And very nice, the for loop runs through every value in the array. 33 00:03:13,180 --> 00:03:14,930 And here is a visual that I made for you. 34 00:03:16,000 --> 00:03:18,460 I start at zero indexing the first element. 35 00:03:19,530 --> 00:03:20,790 Increases up to one. 36 00:03:22,400 --> 00:03:24,020 Indexing the second element. 37 00:03:26,310 --> 00:03:30,630 And the loop keeps running until I equals three, which allows us to index the last elements. 38 00:03:39,720 --> 00:03:42,900 Arrays of a length property that calculates the number of elements. 39 00:03:44,830 --> 00:03:48,250 You can use the length to calculate the number of elements for kingdoms, right? 40 00:03:49,150 --> 00:03:50,260 So here I'm going to print. 41 00:03:53,560 --> 00:03:55,300 The number of elements is. 42 00:03:57,690 --> 00:03:58,830 Kingdoms that length. 43 00:04:11,660 --> 00:04:16,700 And indeed, the number of elements is four, because the array has four elements, so length equals 44 00:04:16,700 --> 00:04:17,029 four. 45 00:04:18,160 --> 00:04:23,710 And so inside the for loop, instead of writing is smaller than four, I'm going to write I is smaller 46 00:04:23,710 --> 00:04:25,060 than kingdoms that length. 47 00:04:34,150 --> 00:04:40,020 And nice, this works very well with that being said, you never need to count the elements yourself, 48 00:04:40,510 --> 00:04:43,510 always use the length property to run through an array. 49 00:05:00,410 --> 00:05:03,420 Now, here's something to consider when you look through an array. 50 00:05:03,440 --> 00:05:08,450 Pay attention to the condition, because what if you replaced your condition with eyes smaller than 51 00:05:08,450 --> 00:05:10,370 or equal to kingdoms that length? 52 00:05:17,820 --> 00:05:21,660 In this case, the code crashes because the next four is out of range. 53 00:05:23,370 --> 00:05:29,040 Because we start counting at zero, the last index number in an array is always going to be one smaller 54 00:05:29,040 --> 00:05:30,240 than the array's length. 55 00:05:35,290 --> 00:05:39,250 Into this loop runs as long as I is smaller than or equal to four. 56 00:05:41,350 --> 00:05:47,200 In the fifth run, the counter is going to equal the array's length, for there is no element at index 57 00:05:47,200 --> 00:05:48,910 four, so the code crashes. 58 00:05:53,270 --> 00:05:57,170 So your loop should only run as long as it is smaller than the array length. 59 00:05:59,710 --> 00:06:04,300 And now everything works, the loop keeps running right up until the last inducts and then the loop 60 00:06:04,300 --> 00:06:05,110 stops. 61 00:06:06,860 --> 00:06:12,020 In this lesson, we used a for loop to access each element in an array, your first for a loop ran through 62 00:06:12,020 --> 00:06:16,940 the length of the kingdom's array and also because we start counting at zero, the last index number 63 00:06:16,940 --> 00:06:19,450 is always one smaller than the array length. 64 00:06:19,910 --> 00:06:24,170 So your condition for running through an array should always be smaller than the length of the array. 65 00:06:30,700 --> 00:06:35,260 If I count up to an index that's equal to the length, your code is going to crash.