1 00:00:00,450 --> 00:00:03,590 We can use a phrase, the store many values at the same time. 2 00:00:05,860 --> 00:00:10,150 I hope you're proud of yourself, because so far you covered most of the fundamentals you need to build 3 00:00:10,150 --> 00:00:15,970 Java applications, namely you learn how to store and update information using variables, control how 4 00:00:15,970 --> 00:00:21,850 your code runs using effluents, which organize your code into functions and running your code many 5 00:00:21,850 --> 00:00:26,620 times using far loops and while loops, the last thing you need to learn about is arrays. 6 00:00:26,980 --> 00:00:30,820 So in this lesson, you're going to use arrays to store many values at the same time. 7 00:00:36,460 --> 00:00:41,650 First thing you need to do is create a new job, a project named Section six, and inside the project 8 00:00:41,650 --> 00:00:47,410 create a new file named intro to Erase Java and as always, make sure the class has a main method. 9 00:00:54,130 --> 00:00:58,240 We can store values into an array, an array lets you store many values at a time. 10 00:01:01,800 --> 00:01:07,530 Arrays in Java look like this, if I want to store many integers at once, then I can put square brackets 11 00:01:07,530 --> 00:01:12,300 next to the integer type and now I can store many integer values in one variable. 12 00:01:14,670 --> 00:01:19,400 That being said, an array can hold many values, but all of them have to share the same type. 13 00:01:19,950 --> 00:01:26,130 For example, integers, stars, an array of integer values, words that stores an array of string values. 14 00:01:27,440 --> 00:01:32,900 Decimals stores an array of double values and letters, stores, an array of characters. 15 00:01:36,070 --> 00:01:39,550 And so from a visual standpoint, this is how a variable stars an array. 16 00:01:46,220 --> 00:01:49,940 Looking at all these arrays, you might be thinking, what is the dot, where did that come from? 17 00:01:50,450 --> 00:01:51,130 Good question. 18 00:01:51,140 --> 00:01:52,010 I'm glad you asked. 19 00:01:52,400 --> 00:01:53,840 The DOT is a reference. 20 00:01:55,880 --> 00:02:00,770 A variable cannot store the array directly at stores, a unique reference, and it's the reference that 21 00:02:00,770 --> 00:02:01,820 points to the array. 22 00:02:02,630 --> 00:02:05,090 This is a really important distinction to make. 23 00:02:05,120 --> 00:02:08,570 I'm going to include these bullet points in your cheat sheet, because I think this is something that's 24 00:02:08,570 --> 00:02:12,770 really important to be aware of, especially as we go through the remaining code. 25 00:02:14,590 --> 00:02:18,580 So instead of your class, you're going to find an array that stores, kingdoms, I'm going to put an 26 00:02:18,580 --> 00:02:20,230 array that can hold many strings. 27 00:02:23,640 --> 00:02:27,330 The variable is going to be called kingdoms and it's going to store for string values. 28 00:02:35,400 --> 00:02:37,530 Northumbria and East Anglia. 29 00:02:41,060 --> 00:02:42,110 Yay for short. 30 00:02:47,820 --> 00:02:49,230 Now, try printing the array. 31 00:03:02,190 --> 00:03:06,720 And what is this seven to six debate, what does this spring represent? 32 00:03:06,750 --> 00:03:07,570 It's kind of weird. 33 00:03:08,220 --> 00:03:09,030 Well, not really. 34 00:03:09,030 --> 00:03:09,630 Think about it. 35 00:03:10,020 --> 00:03:10,910 Pretty line Prince. 36 00:03:10,920 --> 00:03:15,930 Whatever is inside the verbal kingdoms and what's inside the verbal is a unique reference represented 37 00:03:15,930 --> 00:03:16,820 by a white dot. 38 00:03:17,190 --> 00:03:22,380 So instead of actually printing the array value itself, what we're printing is the reference that points 39 00:03:22,380 --> 00:03:22,680 to it. 40 00:03:26,330 --> 00:03:31,910 So how do we access values from an array, how do we access an array elements, you can access an element 41 00:03:31,910 --> 00:03:33,710 by referring to its index number. 42 00:03:34,130 --> 00:03:38,820 By the way, an element is just a value inside of an array in the coding world. 43 00:03:38,840 --> 00:03:40,010 We say elements. 44 00:03:41,060 --> 00:03:44,910 And every element has an index, an index is a number that represents a position. 45 00:03:45,290 --> 00:03:48,360 It starts at zero and counts up the number of elements. 46 00:03:48,800 --> 00:03:50,640 So Jane would have an index of zero. 47 00:03:50,660 --> 00:03:52,220 Joe would have an index of one. 48 00:03:52,220 --> 00:03:53,960 And Joe would have an index of two. 49 00:03:55,190 --> 00:04:00,710 If you want to access an element, just type Geary's name, followed by square brackets and inside the 50 00:04:00,710 --> 00:04:03,860 brackets, write the index of the element that you want to access. 51 00:04:04,490 --> 00:04:07,970 So index is zero would point to the first element in the array. 52 00:04:07,970 --> 00:04:11,300 Jane, index one Joe and index to Joe. 53 00:04:15,360 --> 00:04:20,339 And so back in our code, we can print each Kingdome individually, I'm going to print the kingdom at 54 00:04:20,339 --> 00:04:21,990 Index zero Mersea. 55 00:04:25,170 --> 00:04:26,790 The kingdom at index won. 56 00:04:28,410 --> 00:04:33,720 The kingdom at index two and the one in the next three, I can now run the code. 57 00:04:39,010 --> 00:04:44,590 And as you can see it, access to each element by referring to its index number and then it printed 58 00:04:44,590 --> 00:04:44,710 it. 59 00:04:45,840 --> 00:04:48,740 I prepared this visual for you just so that you can see what's going on. 60 00:05:09,940 --> 00:05:15,460 OK, now, more often than not, you're going to specify an index that's out of range and Jeb is going 61 00:05:15,460 --> 00:05:20,020 to yell at you and crush, you cannot refer to an index number that doesn't exist. 62 00:05:20,440 --> 00:05:25,060 You cannot refer to an index beyond the bounds of the array if you try to. 63 00:05:25,060 --> 00:05:27,190 Jeb is going to throw an error and it's going to crash. 64 00:05:30,640 --> 00:05:36,190 And so the kingdom, right, it had four elements, but because we start counting at zero, the last 65 00:05:36,190 --> 00:05:40,330 element is going to have an index that's exactly one less the length of the array three. 66 00:05:43,580 --> 00:05:47,600 So in your code, if we try to access an element that index for let's do just that. 67 00:05:50,100 --> 00:05:50,940 Run the code. 68 00:06:00,520 --> 00:06:06,670 And Java crashes, the last print statement throws an error because the next four is out of bounds. 69 00:06:07,720 --> 00:06:11,070 You cannot access an element from an index that doesn't exist. 70 00:06:23,200 --> 00:06:26,500 So I'm going to delete the last print statement and clear the output. 71 00:06:34,130 --> 00:06:39,320 In this video, you use the arrays to store many values in the same variable and then you accept each 72 00:06:39,320 --> 00:06:45,380 element using its index, you define a string array that stores kingdoms, each element has an index 73 00:06:45,380 --> 00:06:48,200 and you access each kingdom by referring to the index number. 74 00:06:50,620 --> 00:06:53,650 From a code perspective, the runtime appears as follows. 75 00:07:02,900 --> 00:07:07,940 I also showed you that we can't refer to an index number that doesn't exist, the kingdom's variable 76 00:07:07,940 --> 00:07:13,020 had four elements, and because we start counting from zero, the index ranges from zero to three. 77 00:07:13,610 --> 00:07:17,750 So if you try to access an element, that index for Jeb is going to throw an error and your code is 78 00:07:17,750 --> 00:07:18,530 going to crash.