So far this course we've only dealt with data structures that hold a single value,
in other words, the standard variable.
An integer variable holds any one number for example, an integer whole number and
a string, as you've seen holds only one long sort of sequence of text.
But in this video what we're gonna be talking about is a way
to store multiple values of the same type.
And what we're talking about is using arrays.
So, an array allows you to do just that.
It's really a data structure that allows you to store sequence of values,
that are all of the same type.
So for example, you can create an array of integers,
an array of characters, an array of strings, etc.
This actually works for all primitive types and even objects like the string.
So declaring an array initially is similar to declaring a regular variable.
Let's just have a look at how to do that.
So as you no doubt recall by now,
you can define and regulate integer variables like so.
So in this case we're defining a variable called myVariable of type int and
assign the value of 50 to it.
Now to go and create an array or to define an array, just after the type,
the primitive type, or the object, the class in other words,
we put square brackets like that.
That actually tells the Java compiler that we're not dealing with a regular variable.
We're dealing with an array.
So number one we'll do that, and by doing that we've actually now invalidated this
line of code, because we're trying to store a regular value into an array,
and that's illegal now with Java syntax.
So I'm gonna delete that.
Now that's the standard definition and what we can then do, is on the next line
if we wanted to we could put myVariable = new, then we repeat the top again.
So we put int and the square brackets
we type in the number of elements we want to assign to this array.
So for example, if we wanted to add space for ten elements,
ten individual integers in this array, we'd use the number ten there like so.
So that's actually assigned ten elements, and
of course the other thing we can do much like our regular variable.
We can actually compile this all on to one line like so.
So in this case, we've defined an array called myVariable.
We've told it that it's of type integer.
And we've assigned ten elements to it.
So to be more precise, let's put myIntArray.
We'll call it myIntArray so
it actually makes more sense, cuz that's what it actually is, an array of integers.
Now this number here again is the number of elements,
that's the number of slots or the number in this case
of separate integers that we want to assign to this array.
So accessing these array elements, how do we access them?
Well we do something like this, we can actually, for example,
to save a value we could put something like myIntValue,
square bracket with a number, so 5, equals 50.
So that's actually saving the value 50 into element.
Now you look at that, you think maybe, well that's obviously saving it into
element 5, but it's actually saving in to element 6.
Now, the reason for that is with all arrays in Java,
they always start the count at position 0, at number 0.
So, in other words, if you want to access the very first element of an array,
you start from position 0.
So, in this case, new int 10, so the first element you could use number 0 and
right up until the last element you would actually use number 9 to allocate that.
So in other word instead of starting from 1 and finishing on 10,
you start from 0 and finish on 9.
Sounds confusing but you'll soon get used to that.
If you just remember that you always start counting from 0 in Java with arrays,
it will make a lot of sense.
So in this case, what we've done looking at this code line,
I want you to write the number 5 in square brackets, and it's 0, 1, 2, 3,
4, 5, so that's actually element 6 = 50.
So that's how we're actually assigning that value.
In terms of arrays, again, it's very similar with syntax for other data types.
We could change this to a double, for
example, if we wanted to, And come back here and change this also to a double.
And change the name, variable name.
And that will actually assign an array called myDoubleArray with 10 elements.
And each of those elements could be stored,
could have stored in them a double.
So that's how we go about defining the array and how we go about defining or
storing a value in the array elements.
The other thing that you might want to do is actually retrieve a value from
a particular element.
And to do that we can actually do something very similar.
We can just put, I'll just output this just to show you.
myIntArray, and we'll put 5 there.
And that's going to now dump out, or print to the screen, element 6.
If you run that, again, we're starting the count from 0 in case you wanna run at 6.
We're getting the value 50, which we actually stored in this line here.
That's how you go about actually retrieving the values as well.
Just remember, whatever you define the variable to be,
that's the type of data that will fit in that array.
So you can't, for example, put a double into an integer array,
in much the same way you can't assign a double variable into an integer variable,
it won't work either.
So that was one way to initialize, you could go through, in this case,
if you wanted to initialize every value in the array,
we could go right through it, we could sort of start from myIntArray, 0 = 45.
myIntArray, 1 = 476, etc.
That could become quite tedious.
So actually there's another shortcut that Java gives us.
If we know ahead of time the values we want to actually save in an array,
we can do something like this.
I could put myIntArray = we could put 1, 2,
3, 4, 5, 6, 7, 8, 9 and 10.
But the reason we're getting an error there is we need to actually copy
this into this, at this place here because this is only applicable the first time we
define the variable.
So I'm gonna do something like that and then delete this out.
So what we've actually done here is we've actually defined, we've actually told
Java, these are the numbers we want you to assign into the array.
But we've also indirectly told Java how many elements are in that array
by the actual number of values that are in this curly brace initializer block here,
separated by commas.
So the fact is, there's actually ten elements in there.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Javas look at that and it automatically assigns ten elements to this integer array
and then it also goes through and actually adds the values for
each of those into each array element.
Just to confirm that we could actually print a couple of these out.
Get rid of this double now cuz we're not using that.
And we'll just do a couple, say, position 6 and position 8.
If we run that, And you can see we've got position element 0 is value number 1.
Position 6 or element 6 is 1, 2, 3, 4, 5, 6, as you can see there.
Starting from 0 so it's actually element 0.
So that's 0, 1, 2, 3, 4, 5, 6.
That's why we've got seven.
And again, the element 8 is actually the ninth element,
which would be this one here, which is the number 9.
And that's why the numbers 1, 7, and
9 match up like you can see here on the screen.
And finally there's also a third way to initialize, so if we go back now and
actually just comment that out for now.
I'm just gonna set that up and comment that out like so.
Actually, what I'll do is, I'll just create a new integer outright.
= newInt, tell again so back to our ten elements.
Type them up, they haven't got any values.
And I would delete these output lines now because they don't look appropriate.
We can actually use a for loop to initialize them.
So we could put something like this, we could put for
int i = 0, i less than 10, i++.
They we can actually put myIntValue i = and
lets just say the form is gonna be i*10.
So what we're doing there is we're actually going through each element of
the array starting from element zero with our for loop.
And you can see myIntArray[i].
i in brackets means access element, so
in this case 0 will be the first time it's called through in the loop.
We're assigning i times 10 to it.
We're going right through for the entire array, and going through so in the values.
And just to confirm that we can do a second loop.
For int, i=0, i<10, i++.
And we can do an port, we'll put element zero, or element + i, I should say.
Plus, value is myIntArray should I say I.
So run that now, we should actually get some output.
Element zero.
And you can see the first time we went through, the value was 0.
10, 20, 30, 40, and so on.
And again, the calculation for that is, it's whatever the index entry for
the four loop that we're going through, multiplying that by ten.
So the first time we go through this, we're accessing element 0, because i is 0.
Zero times ten of course is gonna be zero.
And i is set to zero, so that's our element zero.
Next time I go through i is gonna be set to one.
So we're using element one in our array.
So in of when my inter ray is one equals one times 10 which is 10.
And so on as you can see right up until nine where the value is 90.
So that's a great way to initialize it,
if you've got some sort of formula to let you know ahead of time that you can
actually use to assign to the particular element in the array.
One of the other quick things I recommend you do though,
is rather than hard coding numbers here, it could be a good idea to use a method
that actually is given to you automatically with arrays.
And the, it's called the length.
So we do the array name, my int value.length.
I think I said it's a method but it's actually a property.
So it's actually a field that's stored in the, well that comes as part and
parcel of the array.
And length gives us the length automatically so
Java will actually look into the array and decide.
It will tell you what the length of that is.
So the good thing about that is if we change that here and
we also go back in here, and we put my int.length as well.
And we run that, we should get exactly the same result.
As you can see, exactly the same as it was before.
But the good thing also is we can change this so we can make this say 25.
And run it and you can see we got
the values from zero to 24 and that's because we've used the lengths field,
the value that comes back from the array automatically from Java.
Java is determined okay, you've changed the length to 25 elements and you've
automatically set it up so we didn't need to change the code for that to work.
So it's a good idea to use .length with arrays whenever you wanna actually
cycle through or figure out what the length is rather than hard-coding a value.
Because you can see there in this example, I would argue that it's easy to read, but
certainly also it's less work for you to maintain because literally it otherwise,
you'd actually have to change 25.
In this case, we changed from ten to 25 elements,
where we've had to change that code as well.
Which doesn't say much now, but as you start producing more and more Java code,
you'll find that you'll be needing to make these changes in a lot more places and
you'll just be opening yourself up to causing errors,
because one of the things you'll actually get,
you'll actually get a problem if you try and access an element that's not there.
So if we change this and we put 30, we'll actually get an error.
So, if we run this, so
you can see what's happening is we've actually got an exception.
And the exception is array index out of bounds.
And the reason that it's out of bounds is we've actually used a number
that's actually too high.
So we've only actually got 25 elements and
we've tried to access an element greater than 25.
You'll get an error if you do that.
You can only access as many elements as you created.
So if we just undo that change and go back to here and do it,
you probably see it a little bit better.
Cuz is should actually still output the first 25 elements before it crashes in
this case.
So again, the problem here is, that number of elements that were actually defining,
is invalid.
So, we have to go back and change that.
So it's just easy, as you could see,
to make sure that how we use the right number.
But we wanna actually assign the actual number in as few places as possible cuz
that then prevents any weird errors coming up as you can see it came up before.
We started trying to override what we knew were the correct values.
So that's how to go about, in this case, printing out the number.
Now the other thing you can use, of course, is with any variable in Java,
including arrays, is we can those two methods.
So what we can do is, we're gonna take this bit of code here.
We're gonna create a method from it then we're gonna call that method.
So we're gonna do, create a new one,
called public static void printarray.
We're actually gonna send it, our array, and to define that as a parameter,
as an argument, we put int same definition before the square brackets and
we'll call it array.
So, if we paste in our code and what we'll need to do is we'll need to just
change the definition to match the parameter like so.
And then now we should just be able to call our method print array, and
pass our array that we've defined called modprintarray.
So it's doing a very similar thing, but we're just passing the array.
Notice on the definition that it's actually the code that's calling
the method, we don't put square breakfast in there, if we do that,
we'll get weird things happening, we'll get an error.
We only define the type in the parameter, as you can see there.
If we run that we should get identical results printing out,
if you can see we're getting.
That's how to go about actually showing it all printing out and
passing it array as a method.
Okay, so what I'm going to do it delete this on the screen now.
I'm gonna show you another use.
So what we're going to do is just delete all that out.
I'm gonna actually show you a real life use of using an array where it would
become valuable.
What we're going to do is we're going to accept some input from the consults.
In other words, allow the user of the program to type in some numbers.
We're gonna sum up those total numbers, and
figure out the averages of those numbers that were typed in.
This could be a good use of an array, this we're dealing with multiple numbers, and
that's where arrays are really great.
So firstly, let's talk something called about scanner.
And we'll talk about that shortly what it is but private static scanner.
Case is important here.
You can see java.util is coming up automatically.
Any time you see something that starts with java., in this case .util,
that's something that comes with Java.
And we're gonna call that Scan.
And, let's call it Scanner.
You can see it's asking to accept the import so I'm gonna do that.
And we'll call it Scanner.
And equals new Scanner.
And we pass a parameter called system.in.
Now, you've seen system.out before where we've actually dumped code.
We do that all the time, you can actually see that to the right on the screen now,
we used system out for that.
System.in is the opposite in that it allows
you to type input into the console that gets returned back to the program.
So that's what we're doing there is we're defining the static variable,
or called scanner.
And we're gonna be using that in the code to actually retrieve that.
So what we're going to do is we're going to start off, we're gonna put int,
array int.
We're gonna call it my integers, equals get integers, five.
So what that means is we're gonna actually create a method.
And that methods is gonna automatically allow us to type in five numbers and
to then return those five numbers in the form of an array.
And those contents will be stored in my integers arrays.
To do that, public.static.int[] array int,
two square brackets means we're returning an array.
Getintegers[int number] and the number in this case is the number
of array elements we that we wanted to find for our array.
So we're gonna print out a message first and
the message is going to put Enter, plus number, integer values.
We're gonna put backslash R there, so it goes to the next line and
you'll see why shortly when we're running it.
Next we need to define our array.
We're gonna call it values equals new int number.
So I would pass, except using the number that was passed source in this case
number, and that's how many elements were assigned to this array.
By doing it this way we've got some flexibility in that this will be able
to accept any number.
We can chop and change that as needed.
Then we just need to create a for loop for int, i = 0,
i less than values.length i++, then we assign the value now
we put values, which you've seen before, values i.
So starting on element 0, we're going to go through the entire array equals and
it's gonna be scanner.nextInt.
And the scanner.nextInt automatically accepts an integer from the console and
that then returns an integer, and that's going to be stored in our array.
And finally, it's going to return the array back to the calling process.
So in this case, what we're doing is we're defining an array called myIntegers, and
the actual value of the array is being passed back from this method
automatically, and it's gonna be assigned to myIntegers.
So at the end of this, after it's made this call, not only have we defined
the length of the array, but we've also got the contents of it as well.
Just to confirm that then, we're going to put a for let there for
int = 0, i less than myIntegers.length, i ++.
And we're just gonna print that message out to see what the top value was.
Let's just run this and see that it works.
First, it's asking us to enter five values.
You'll notice when I clicked my mouse in there,
it automatically went to the next line.
If I didn't put the \r here, it would be actually asking for
input as the first one there, which we didn't want.
We actually wanted it on this line, that's the reason I put that \r there.
So let's enter five values, so 10, 26, 52, 66, and the last one 23.
You can see element 0, type value was 10, 22, 52, 66, 23.
So you can see that this has returned this array and
we've got the correct values in there.
So we're nearly done, so we've actually got the values.
The next thing we need to do is actually create another method.
And this method we're gonna pass the array again.
And we're gonna get it to return what the actual average of the numbers are.
So again the average is calculated by determining the sum
of all the values in the array and dividing that by the actual length.
So let's do that.
We're gonna do public, static, double.
We'll call this one getAverage.
And then we need to pass our array.
So it's going to be our, we'll call this one array.
And we're returning a double because with an average we may actually
get a number that's not a whole number as the result.
So, we're going to make sure that the number that we return can cope with whole
numbers or, in the case of an average being another whole number,
show that correct value as well and we'll see that in operations shortly.
We need to define a local variable that is the sum total.
I'm gonna call that sum and set that to 0.
We're gonna create a for loop again,
for(int i=0; i< array.length, i++).
We're gonna go through the entire array.
Then we need to assign whatever the current value is of the current array
element, as we cycle through using the for loop.
Need to add that to sum so we have a += array and you know the element, the +=.
We didn't put a +=, of course we wouldn't be recording the other values,
we'd be overwriting it each time, += means that we're actually adding to it.
So that's good.
So we now got the total sum is now stored in the variable called sum.
Now to figure out the,
to return the average is simply dividing sum by the length.
But then we wanna convert the result to a double, so
we're gonna put return double sum, divided by double array.length.
Again using doubles to make sure that we're actually returning a double, it's
just dividing the total sum of all the values in the array by the actual length.
The last thing we should need to do is just update our main method and
print out a message, to what time you like.
The average is getAverage and pass our array.
So if we run that, And
we store our three values, something like 3, 4, 5, 6, 7.
And put the average is, we should have put a space there.
Let's roll that again.
3, 4, 5, 6, 7, the average is 5.0.
So 7 plus 6 is 13 plus 5 is 18 plus 4 is 22 plus 3 is 25.
So that's 25 is our sum there.
And the five elements, 25 divided by 5 is 5.
And notice also, that IntelliJ, is showing you these numbers in green, which is its
way of saying that these numbers were actually typed in from the console,
which is pretty handy.
So that's our function, getAverage,
which actually returns an average of the total number of elements.
And just one last example just to show the definition of why we needed a double,
so enter one more number.
Let's use a number that we know is not gonna evenly divide.
So we'll say 100 + 50, 15, so 5 and a 2 and
that would give us 34.4, cuz that's 150, 165, 172.
172 divided by 5 is 34.4.
So that's why we needed to use the double there.
So this video has gone way longer then I had hoped it [LAUGH] would go for.
So I hope you got a lot out of that.
In the next video, what we're gonna do is we'll do our challenge for arrays, and
we'll talk about that and see you in the next video.