Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Arrays!
You have completed Java Arrays!
Preview
You are probably wanting to use these right? Let's learn how and also take a look at varargs.
Learn More
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We haven't yet touched on how
to use arrays in your existing application
0:01
methods.
0:05
You'll definitely start seeing them return
from methods in the standard library,
0:06
and I'm guessing you'll probably
want to start using their abilities.
0:11
When you declare your methods,
you have a couple of options
0:14
to help users of your program,
or API, understand what you're expecting.
0:17
Now, aside from the typical type
declaration, there's a very clear pattern
0:22
I'd like to show off, so let's get to it.
0:26
In the standard boilerplate
0:30
Java program, you notice
that there's a public static void main.
0:31
You can see what it looks like
to declare a method that expects an array.
0:35
You can definitely do this too.
0:39
You just make a new method and define
your parameter to be a typed array.
0:41
Just like this array here named args.
0:46
Now this communicates
to users of your code that you expect them
0:48
to create an array of the specified type
and pass it into your method.
0:52
Now on the flip side, you can always
return arrays from a method.
0:56
You've most likely seen this before.
1:00
So for instance, on string, there's a method that returns an array of characters.
1:02
I'm going to get JShell up and running
here.
1:08
Let's
1:15
go ahead and we'll open that Scratch up.
1:16
So we'll do a slash open Scratch.java.
1:19
I want to make sure that we have
this arrays import there for us.
1:22
All right, so we'll go ahead
and we'll declare a new char or car array,
1:26
as I've been told,
because this is for character.
1:30
People say that it's not "chair-rector",
so car,
1:33
chair, care, I don't know, whatever.
1:36
A character array
1:41
called letters,
and we'll use treehouse here.
1:42
The string treehouse,
and it has toCharArray here.
1:45
And it's pretty clear
that that returns a character array,
1:49
and it will send it into letters.
1:51
So now that we have an array of letters,
1:55
we can use it
just any other array that we had.
1:56
So we could use the sort that we just saw.
1:59
So we could say arrays.sort letters.
2:02
And what this will do is it will arrange
those characters in such a way.
2:06
So now the letters.
you see here instead of treehouse
2:09
we have "eeehorstu"...
whatever that means.
2:12
So let me show you another way
to get an array into your method.
2:17
Now I quite like this approach,
and I think you will too.
2:21
So let's say
that we wanted to create a new method
2:24
that allows you to randomly
pick something to make for dinner.
2:26
Now, I don't know about you,
but this is a common problem for me.
2:30
What should we eat?
2:33
Everyone has their preferences.
2:35
There's a lot of input,
but not a lot of decision.
2:36
So let's make a method to solve
this problem.
2:39
Let's do it right here in Scratch.
2:42
So one thing that you might not know
is that JShell lets you create methods
2:44
kind of wherever.
2:48
So we'll make a new public method
that returns a string.
2:50
And we'll make this
choose dinner for us, right?
2:53
Now I could just do this
2:57
and make people say,
hey, pass us in an array of meals.
2:58
But that would mean
that they'd have to build them first.
3:02
And I don't think we want that.
3:04
We don't want them to have to do that.
3:06
So one thing that you can do is use
what is known as var args.
3:08
And what that does
is it allows users to provide zero
3:12
or more of this type of variable.
3:15
So it looks this.
3:18
Instead of the square brackets,
we'll just go dot, dot, dot.
3:20
Kind of like an ellipsis.
3:24
And then in your method here,
what happens is meals is an array
3:26
that's available to you.
That's pretty cool, right?
3:29
So let's finish the method body here
so you can see.
3:32
So let's print out how many we got.
3:35
So we can say system.out.printf.
3:37
We'll say randomly choosing
3:42
%d dinner options.
3:45
And we'll make a new line there.
3:49
Okay, so it's an array,
so it has a length.
3:53
So it says meals.length
and then we'll fill in that.
3:56
Awesome. Okay.
4:00
Now why don't we return a random element
from this array of meals?
4:01
Now to do this, we'll need to import
another utility class named random.
4:05
So let's just do that here.
We'll say import java.util.Random.
4:10
And then to use it in your method
you just create a new
4:16
one of these things called random
So we'll say random,
4:18
and we'll just call it random, lowercase,
4:22
equals a new random.
4:25
That's a lot of random, I know.
4:29
Now random has a pretty great
method named nextInt.
4:31
That gets an integer for you between zero
and some upper bound.
4:35
Now this is perfect for us
because arrays start at zero,
4:39
and the random number will go up
until the number that you specify,
4:42
which is pretty perfect
for choosing a random array index.
4:46
So what we'll do is
this, we'll return a random value
4:50
from our meals
array by using random.nextInt.
4:52
That will
4:56
give us zero up until
the top of the array, right?
4:57
Because we don't want to give outside
of the meals.length.
5:00
And we'll close that out.
5:04
Cool.
5:05
It excludes the top number, so remember,
5:07
whenever we're looping,
we're doing less than meals.length.
5:09
This is kind of the same thing.
5:12
It will not include the top of however
many meals there are, one less than that.
5:13
So now that we have the method, let's
go ahead and save this
5:18
and reopen up our scratch.java.
5:21
And now let's call our method.
5:27
It's just here in the global jshell space.
5:29
So we called it chooseDinner.
5:32
Now I can pass however many values I want.
5:34
So as always,
I could go for a nice burrito.
5:37
Mmm, how tasty.
5:39
I know my wife would prefer some pasta
though.
5:42
And my kid can't speak yet,
but I know he'll definitely want some
5:46
oatmeal with blueberries.
5:49
Alright, so what will
5:53
happen when we run
this is we'll get an option returned.
5:53
So here we go.
5:57
Choosing from three dinner options,
it picked pasta.
5:58
That's great.
6:01
So if I do up arrow, you'll see that
we will get a different random one.
6:02
Oatmeal with blueberries.
6:05
And then, let's do it one more time.
6:07
We got pasta again. So see, it's random.
6:09
Cool, right?
6:12
And what I really is
that the users of our method
6:13
don't need to go create a new array.
They can just pass in their options.
6:16
They don't even need to know
that we're using an array.
6:19
Now one thing you want to remember
is that this array can actually be empty
6:23
meaning it can have no elements at all
VarArgs allows for that.
6:27
It's zero to many.
6:30
So here, let's check this out.
6:32
Let's go and
see what happens if I do a chooseDinner
6:34
and I do zero arguments, which is allowed
because that's what VarArgs says.
6:37
Oh, illegal argument exception.
6:41
Bound must be positive,
because it's zero, right?
6:45
Var args allowed it,
but our next int call doesn't it.
6:48
So we need to make sure
that we protect against this.
6:52
And in our use case here,
I think the answer is pretty obvious.
6:55
What happens
if meals.length is equal to zero?
6:58
If nobody suggested anything, right?
7:02
Nobody said any sort of meal.
7:04
We're just going to return
the only correct answer possible.
7:06
Tacos, silly.
7:10
And that is final.
7:12
I'll probably never add arguments
to this method ever again though.
7:14
And let's go ahead and save
7:18
and reload with slash open scratch.java
7:20
and if we call
7:26
chooseDinner, tacos silly.
7:26
Excellent, it knows me so well!
7:30
now if that example was a little too silly
for you, let's take a quick gander
7:33
at one that we've been using
system.out.printf.
7:37
I'm just going to jump over here to Google,
and I'm going to search for Java printf.
7:41
Cool, here's Oracle.
7:47
And if we look right
7:53
here, we'll see it
says, these methods, format and printf,
7:53
are equivalent to one another,
and that they are of type printstream.
7:58
Hey, look at that, dot, dot, dot.
8:01
So that's far args in action.
8:05
because you don't know how many %s or %d
you're going to put into this format,
8:07
and then you just pass it
whatever you want to do there.
8:12
That makes sense, right?
8:15
There's no VAR arguing about it.
8:17
This is a pretty great feature
when you need it.
8:18
One thing to note, it always has to be
the final parameter that you declare.
8:21
Otherwise, how would it know when to stop?
8:26
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up