Array Literal Shortcut3:49 with Craig Dennis
Let's explore a handy way to create arrays when you know all the values at creation time.
We currently have two array examples that we've been exploring. 0:00 They're pretty similar but slightly different. 0:03 Now first, we have golf scores. 0:06 We know that we have a fix number of holes, 18. 0:07 But we don't know what those scores are the moment that we create scores array. 0:11 Our other example, the friends going to a movie together. 0:16 We actually do note who the friends are at creation time. 0:19 Now when you know all the values of an array, 0:22 you really should use what is known as an array literate. 0:24 It saves a lot of typing and it's actually quite concise. 0:27 You're gonna love it. 0:30 Let's go take a look. 0:31 So go ahead and get your workspace up and running with jshell. 0:32 The declaration of the array actually still looks the same. 0:36 So we have a String of friends. 0:40 Now what looks different is the way that we set the values. 0:42 So what we do is we open up with a curly brace, and 0:45 let me type in the values separated by comas. 0:48 Now, note that these are curly braces other programming languages tend to 0:55 represent this bit here as a hard bracket, but Java uses curly braces. 1:01 Now, that's pretty clear, isn't it? 1:06 So, it sets each index in the order of the values here, right. 1:09 So this is 0, 1, 2, right? 1:13 So, we can do friends 0, get the first one out, and then it's gonna be Pasan. 1:15 And we can use the up arrow, we can look and we can get Alena out too, right. 1:22 And also, what happens is it automatically get set, 1:27 the length to the amount that you passed in, automatically. 1:31 Pretty handy, right? 1:36 So one thing I wanted to point out that this declaring and 1:37 initializing on the same line, gave us some extra powers. 1:40 If you declare that array first and 1:44 don't initialize it, you need to also declare its type, so let's do that. 1:46 So, let's say that I wanted to get snacks from the snack, so 1:51 we're gonna have an array of snack name and so we'll call it snacks. 1:54 Okay, and I'm not gonna initialize it, just declare it. 1:58 And then later let's say that I wanted to use it. 2:01 Now, one would assume that you can probably just say snacks =, 2:03 let's see, we wanna gonna get nachos. 2:09 We want some Sour Patch Kids, obviously, 2:13 probably need that Snickers, and I think we need a large popcorn. 2:17 Here we go, that sounds good. 2:24 So you would think that you could just do this, 2:26 but what you'll see is you'll get this illegal start of expression. 2:29 So this'll happen to you for sure. 2:33 Now, what's happened here is on the one liner this type has been inferred, 2:34 it could figure it out automatically. 2:40 Here though not so much, but here's the work around. 2:43 So I'm gonna go ahead, I'm gonna press the up arrow. 2:45 And I will preface this, move our cursor over here, 2:49 we're gonna preface this with a new string array. 2:53 Here we go, and now it knows the type and now it will work fine. 3:01 So this typed literal style is also a great way 3:05 to pass an array into a method that takes an array as a parameter. 3:08 This way you don't need to create a new variable, you can just do this. 3:12 You can just say new string and then curly braces the values, there you go. 3:15 That you way, you wouldn't need to create a variable. 3:20 And it makes an anonymous or unnamed array for you. 3:22 Well, we'll explore this in a bit. 3:26 This next looks so good. 3:28 I guess I should've asked the others what they wanted. 3:30 So now that we know how to declare, initialize, and 3:33 access elements of the array, we are ready to start using them more programmatically. 3:36 We have this wonderful populated data structure 3:41 now all stored in a single variable. 3:44 Why don't we start looping through it showing off one of its main benefits. 3:45
You need to sign up for Treehouse in order to download course files.Sign up