Array Usage in Method Declarations9:35 with Craig Dennis
You are probably wanting to use these right? Let's learn how and also take a look at varargs.
We haven't yet 0:00 touched on how to use arrays in your existing application methods. 0:00 You'll definitely start seeing them return from methods in the standard library. 0:04 And I'm guessing you'll probably want to to start using their abilities. 0:08 When you declare your methods, you have a couple of options to help 0:12 users of your program, or API, understand what you're expecting. 0:15 Now aside from the typical type declaration, 0:19 there's a very clear pattern I'd like to show off. 0:22 So let's get to it. 0:24 In the standard boilerplate java program, 0:26 you notice here there's public static void main. 0:28 You can see what it looks like to declare a method that expects an array. 0:32 You can definitely do this too. 0:35 You just make a new method and define your parameter to be a typed array, 0:37 just like this array here named args. 0:42 Now this communicates to users of your code, that you expect them to create 0:45 an array of the specified type, and pass it into your method. 0:49 Now on the flip side, you can return arrays from a method. 0:53 You've most likely seen this before. 0:57 So for instance, 0:59 on String, there is a method that returns an array of characters. 1:01 I'm gonna get jshell up and running here. 1:06 And I'm gonna get back over here to scratch. 1:12 Let's go ahead and we'll open that scratch up. 1:17 So we'll do a slash, 1:19 open scratch.java. 1:23 I wanna make sure that we have this arrays import there for us. 1:27 So, let's move this up a little bit, and 1:32 I'm gonna do control l to get us up to the top here. 1:35 All right, so we'll go ahead and we'll declare a new char or 1:38 car array, as I've been told because this is for character. 1:43 People say that it's not chair-rector, so car, char, care, I don't know, whatever. 1:47 >> [LAUGH] Character called letters. 1:54 And we'll use tree house here, we'll say tree house. 1:57 The string tree house, and it has two char array there. 2:02 And it's pretty clear that that returns a character array. 2:06 And it will send it into letters. 2:10 So now that we have an array of letters, 2:11 we can use it just like any other array that we had. 2:15 So, we could use the sort that we just saw. 2:18 So, we could say Arrays.sort letters. 2:20 And what this will do is it will arrange those characters in such a way, 2:22 so now the letters. 2:27 You'll see here instead of treehouse, we have e hors tu, e hors tu, mama tambian. 2:29 That was a weird joke that didn't translate very well. 2:35 So, let me show you another way to get an array into your method. 2:40 Now, I quite like this approach and I think you will too. 2:44 So, let's say that we wanted to create a new method that allows you to randomly 2:46 pick a place to go eat lunch. 2:50 Now, I don't know about you, but this is a common problem for 2:52 us here at the Treehouse office. 2:55 Where should we go eat? 2:57 Everybody has their preferences, there's a lot of input, but not a lot of decision. 2:58 So, let's make a method to solve this problem. 3:02 Let's do it right here in scratch. 3:06 So, one thing that you might not know is that jshell let's you 3:08 create methods kinda wherever. 3:12 So we'll make a new public string, and 3:14 we will make this pick a lunch spot for us, right? 3:18 Now I could just do this and make people say, hey, 3:22 this is pass us in an array of lunch spots. 3:26 But that would mean that they'd have to build them first, and 3:29 I don't think we want that. 3:31 We don't want them to do that. 3:32 So, one thing that you can do is use what it is known as var args. 3:33 And what that does is it allows users to provide zero or 3:39 more of this type of variable. 3:42 So what you do, what that looks like is this. 3:46 So you just go dot dot dot, kind of like ellipses there. 3:48 And then in your method here, 3:53 what happens is its spots is in array that's available to you. 3:55 That's pretty cool right, so let's finish the method body here so you can see. 3:59 So let's print out how many we got. 4:03 So we say system dot out dot print f. 4:06 We'll say randomly picking percent d, lunch spots. 4:09 And we'll make a new line there. 4:18 Okay, so it's an array, so it has a link. 4:22 So it says spots dot link, then we'll fill in that. 4:24 Awesome, okay, and now why don't we return a random element from this array spot. 4:28 Now, to do this, we'll need to input another utility class named random. 4:34 So lets just do that here, we'll say, 4:39 import java dot util dot random. 4:44 And then to use it in your method, 4:48 you just create a new one of these things called random. 4:50 So we'll say random, and we'll just call it random equals new random. 4:54 Now random has a pretty great method named next int, 5:00 that gets in integer for you between zero and some upper bound. 5:04 Now this is perfect for us because arrays start at zero. 5:09 And the random number will go up and tell the number that you specify. 5:12 Which is pretty perfect for choosing a random array index. 5:16 So what we'll do is, we'll return a random value 5:22 from our spots array by using random dot nextInt. 5:27 And that will give us zero up until the top of the array, right? 5:33 Cuz we don't wanna give outside of the spots dot length. 5:37 And we'll close that out, cool. 5:44 And it excludes the top number, so remember, 5:46 whenever we're looping we're doing less than spots dot length. 5:48 This is kind of the same thing, 5:51 it will not include the top of however many spots there are, one less than that. 5:52 So now that we have the method, let's go ahead and re-open up our scratch dot java. 6:00 And now, let's call our method, it's just here in the global jshell space. 6:07 So we called it pickLunch Spot. 6:11 Now I can pass however many values I want. 6:18 So for now, I could go for a nice burrito 6:21 over at Que Sabrosa, mm how tasty. 6:26 Or we could go to our favorite Peruvian restaurant Las Primus, get a lomo saltado. 6:30 We go there all the time, they're like family. 6:36 Or a very specific pun-based pizza place called Life of Pie, get it? 6:39 All right, so what will happen when we run this is we'll get an option. 6:47 So here we go, picking from three lunch spots, 6:54 it picked Life of Pie, that's great. 6:56 So, if I do up arrow, you'll see it will get a different random one. 6:58 Las Primas, and then, let's do it one more time. 7:02 We got Life of Pie again, so see, it's random. 7:04 Cool right, and what I really like is that users of our method, 7:06 don't need to go to create a new array, they can just pass in their options. 7:10 They don't even need to know that we're using an array. 7:15 Now, one thing you wanna remember is that this array, 7:18 can actually be empty, meaning it can have no elements at all. 7:22 Varags allow for that, it's zero to many. 7:25 So, right now, here, check this out. 7:28 Let's go and see what happens if I do a pickLunchSpot and 7:30 I do zero arguments, which is allowed because that is what var arg says. 7:34 Aw, illegal argument exception bound must be positive because it's zero, right? 7:41 So, we need to make sure that we protect against this. 7:46 And in our business use case here I think the answer is pretty obvious. 7:50 What happens is if spots dot length is equal to zero. 7:53 If nobody suggested anything, right, nobody said any sort of lunch spot, 8:01 we're just gonna return some place. 8:06 With tacos, that is a good default lunch. 8:10 And let's go ahead and reload, get to our open scratch, 8:15 and if we call pick lunch spot, some place with tacos, excellent, nailed it. 8:22 Now, if that example was a little too silly for you, let's take a quick 8:28 gander at one that we've been using, system dot out dot printf. 8:31 I was just going to jump over here to Google, and 8:36 I'm going to search for Java 8 system. 8:39 Cool, so we're gonna jump in the Java docs here. 8:45 And if we scroll down here to out, we'll see that that is of type print stream. 8:48 So we go in there, let's click it, nice. 8:53 And now if we scroll down to instance methods here, 8:55 so look at the instance methods. 8:59 And we know that we're using print f. 9:02 And the one that we're using is right here, we're using the string format, and 9:06 we're using object, hey look at that, dot dot dot. 9:08 So that's var args annexion. 9:13 Because you don't know how many percent S's or percent D's you're gonna put in 9:14 this format, and then you just pass whatever you want to do there. 9:19 Makes sense, right, 9:23 there is no var arguing about it, this is a pretty great feature when you need it. 9:23 One thing to note, it always has to be the final parameter that you declare, 9:28 otherwise how would it know when to stop? 9:33
You need to sign up for Treehouse in order to download course files.Sign up