1 00:00:00,000 --> 00:00:04,845 So if your workspace timed out, you might not be able to access your jshell history, 2 00:00:04,845 --> 00:00:07,792 normally you can just do that by using the up arrow. 3 00:00:07,792 --> 00:00:12,562 Let's go ahead and practice building our friend's array all over again. 4 00:00:12,562 --> 00:00:13,890 So let's see. 5 00:00:13,890 --> 00:00:20,276 I want to say that the type is a String array named friends and 6 00:00:20,276 --> 00:00:26,210 I want a new string array with three elements, right? 7 00:00:26,210 --> 00:00:27,646 And there we go, 8 00:00:27,646 --> 00:00:33,502 we have an array with each element set to the types of default value. 9 00:00:33,502 --> 00:00:38,760 So we wanna set this first value, the first value here, this first null. 10 00:00:38,760 --> 00:00:40,985 We wanna set that to Pasan, so 11 00:00:40,985 --> 00:00:46,592 the way to access an element of an array is by using what is known as an index. 12 00:00:46,592 --> 00:00:52,080 Now one of the more challenging things for you to remember is that arrays 13 00:00:52,080 --> 00:00:57,951 are zero based and that means that their indexes or indices start with zero. 14 00:00:57,951 --> 00:01:01,222 So the first elements index is actually zero. 15 00:01:01,222 --> 00:01:04,411 So here, lets set the first element to Pasan. 16 00:01:04,411 --> 00:01:07,168 So first we mention the array, so friends. 17 00:01:07,168 --> 00:01:12,388 And to specify which element we are referring to we can add a hard bracket 18 00:01:12,388 --> 00:01:17,620 followed by the index, which we want to set the first one which is zero. 19 00:01:17,620 --> 00:01:21,832 Cuz counting starts at zero with arrays, so we are gonna say Pasan. 20 00:01:24,958 --> 00:01:27,866 There we go, and now if we take a look at our array here, 21 00:01:27,866 --> 00:01:31,433 we can just type it here, it will return what the value looks like. 22 00:01:31,433 --> 00:01:34,332 We'll see that Pasan's on the first element there. 23 00:01:34,332 --> 00:01:37,633 Now, I realize that it's weird that the first element is zero. 24 00:01:37,633 --> 00:01:40,544 But if you think about it, we kinda do that too in English. 25 00:01:40,544 --> 00:01:44,935 So, take a moment and think about how we report on age. 26 00:01:44,935 --> 00:01:47,392 Now my youngest daughter is about to turn three. 27 00:01:47,392 --> 00:01:50,981 Her first year of life though, like before her first birthday, 28 00:01:50,981 --> 00:01:53,783 we talked about how old she was in terms of months. 29 00:01:53,783 --> 00:01:57,211 It was never like, hey how old is this cutie? 30 00:01:57,211 --> 00:02:01,352 Zero, no, but it was really her zeroth year. 31 00:02:01,352 --> 00:02:05,730 Now after she turned one, we just say one and now she's two. 32 00:02:05,730 --> 00:02:07,795 We don't really ever say that, 33 00:02:07,795 --> 00:02:12,013 but to access that first year of life by index I would use zero too. 34 00:02:12,013 --> 00:02:16,836 Now as weird as it sounds in baby years it's okay to say zeroth in 35 00:02:16,836 --> 00:02:20,661 this array world no matter how awkward it sounds. 36 00:02:20,661 --> 00:02:24,637 This starting at zero is such a confusing thing to remember and 37 00:02:24,637 --> 00:02:26,553 its the cause of a lot of bugs. 38 00:02:26,553 --> 00:02:30,743 Now I'll do my best to hammer this home and by the end of the course I hope to 39 00:02:30,743 --> 00:02:34,960 have man you enough about it to make sure that it comes as a second nature. 40 00:02:36,210 --> 00:02:40,422 So I guess that'd be index one in the old nature array. 41 00:02:40,422 --> 00:02:43,880 Okay, so next in our array we want to add Alena. 42 00:02:43,880 --> 00:02:46,232 So she would be the second element in the array. 43 00:02:46,232 --> 00:02:49,490 So that would be index 1. 44 00:02:49,490 --> 00:02:57,073 So we'll say friends[1] is Alena. 45 00:02:57,073 --> 00:03:00,302 There we go and I refreshed the screen with Ctrl+L and 46 00:03:00,302 --> 00:03:05,000 now if we take a look at friends here, we'll see that we have Pasan and Alena. 47 00:03:05,000 --> 00:03:08,193 And we can access those values using the same indexing methods. 48 00:03:08,193 --> 00:03:13,694 So if I say system.out.print.ln and 49 00:03:13,694 --> 00:03:17,195 we're gonna go ahead and 50 00:03:17,195 --> 00:03:22,371 say (friends[0] + is awesome! 51 00:03:25,710 --> 00:03:29,511 We're accessing the values using the same indexing. 52 00:03:29,511 --> 00:03:33,884 And if I use the up arrow we can replace this with. 53 00:03:36,598 --> 00:03:41,083 1, Alena is awesome, which is true. 54 00:03:41,083 --> 00:03:45,390 I wonder what happens if we give it an index that doesn't exist in our array. 55 00:03:45,390 --> 00:03:46,964 Now we only have three elements. 56 00:03:46,964 --> 00:03:53,498 So what would happen if I came in here and made this a 4? 57 00:03:53,498 --> 00:03:56,602 ArrayIndexOutOfBoundsException. 58 00:03:56,602 --> 00:03:59,932 So how do you know what the upper bound of an array is? 59 00:03:59,932 --> 00:04:03,838 Well, arrays have a handy public attribute, which is final, and 60 00:04:03,838 --> 00:04:05,111 it's named length. 61 00:04:05,111 --> 00:04:10,181 So if I say friends.length, I'll see that I have three. 62 00:04:10,181 --> 00:04:15,162 So you always wanna make sure that your index is less than the length. 63 00:04:15,162 --> 00:04:16,191 So let's do this. 64 00:04:16,191 --> 00:04:18,722 I'm gonna clear the screen again, that's Ctrl+L. 65 00:04:18,722 --> 00:04:20,032 So let's do this. 66 00:04:20,032 --> 00:04:22,451 Let's set the third element to Ben. 67 00:04:22,451 --> 00:04:24,582 Well, actually, why don't you try it yourself? 68 00:04:24,582 --> 00:04:30,616 Why don't you set the third element 69 00:04:30,616 --> 00:04:35,453 of the friends array to Ben. 70 00:04:35,453 --> 00:04:38,189 Now go ahead and pause me and after you get it unpause me and 71 00:04:38,189 --> 00:04:39,501 I'll show you how I did it. 72 00:04:39,501 --> 00:04:41,642 So go ahead and pause me. 73 00:04:41,642 --> 00:04:44,010 Okay, did you fall for it? 74 00:04:44,010 --> 00:04:47,752 The third element is actually index 2, right? 75 00:04:47,752 --> 00:04:53,302 So if we say friends 2 equals Ben, right? 76 00:04:53,302 --> 00:04:56,389 The last element in our friends is at index 2, 77 00:04:56,389 --> 00:05:00,600 therefore it's 1 less than the link which remember, was 3. 78 00:05:00,600 --> 00:05:06,861 So you can always get the last element in an array by subtracting 1 from its length. 79 00:05:06,861 --> 00:05:10,611 Now you can actually put expressions in our brackets as well. 80 00:05:10,611 --> 00:05:19,197 So I can say friends, friends.length which really is three, we know that minus 1. 81 00:05:19,197 --> 00:05:21,483 And what will happen is that we'll calculate and 82 00:05:21,483 --> 00:05:22,940 then we'll get the value out. 83 00:05:25,230 --> 00:05:29,754 Now in this example, I already knew which of my friends were coming along with me. 84 00:05:29,754 --> 00:05:33,173 So assignment like this, by index seems kinda silly. 85 00:05:33,173 --> 00:05:36,906 It feels like I should be able to just set that when I create my friends 86 00:05:36,906 --> 00:05:37,833 array, right? 87 00:05:37,833 --> 00:05:39,362 Well good news, you can. 88 00:05:39,362 --> 00:05:42,773 It's a bit of a shortcut and it's known as an array literal. 89 00:05:42,773 --> 00:05:44,900 Let's look at that right after this quick break.