Alright, so let's recap what we've
learnt so far about arrays and discuss
some of the common errors that occur
with arrays. So an array is a data
structure that allows us to store
multiple values of the same type into a
single variable. The default values of a
numeric array elements are set to zero
arrays are zero indexed, so an array with
n elements is indexed from 0 to n take 1.
So for example 10 elements the index
range would be from 0 through 9. Now if
we try to access an index that is out of range
Java will give us an array index out of
bounds exception that which indicates
that the index is out of range in other
words, out of bounds we're trying to
access an element that is outside of
what we've defined. To access array
elements, we use square braces this is
also known as the array access operator.
So let's see an example of how to create
an array, so we use square brackets after
the type in this case it's an int array
so we've got int square braces then the
name of variable in this case array so
it's in square brackets array then after
the equal sign we type new int in this
case since it's an int array and 5 and
square braces representing the number of
elements in the array, in other words the
length of the array. So you can see in
this scenario that the array contains
the elements from array 0 through to
array 4, it's got 5 elements and the
index range in this example is 0 to 4
the new operator or keyword is used to
create the array and initialize the
array elements to their default values,
remembering that an int in this case
will be defaulted to 0. Now for boolean
type arrays if we use boolean instead of
int they would all be initialized to
false, if you use string or other objects
they would be set to no but we'll be
talking more about null and what that is
later in the course. So this example here
we've got int left to right square
braces my numbers equals and we've went
left to right curly braces with the
numbers 5 4 3 2 1 in it then at the end
as a semicolon. So this is a way to
initialize an array inline using an
array initializer block and that's what
that is there the left and right curly
braces with further
contents in between and notice that
we're defining the values for the array
separated by a comma. So this way of
initializing is also known as an
anonymous array it's got five elements
and the index range is 0 through 4 and
in this example you've probably figured
out that the array and the elements are
initialized to 5 4 3 2 1. Alright,
here's another example of our array in
square braces my array equals new int 5
and doubled in square braces and a
semicolon you may recall that when we
first used variables, a set of variable
is a place in memory, so think of it like
a box that you could store something and
you can store something in that box, well
the same goes for arrays but with arrays
you can imagine an array it's a row of
boxes of slots where each box can
contain a value, so in other words we
could store something into each box so
in this example I mentioned we created
an array with five elements so in other
words five boxes and they are by default
all initialized to zero, so every box in
this example contains an integer value
of zero, so every box or slots got an
index so that we can access the values
by an index using square braces, so in
this example we've initialized the array
with values in the same line using an
array initializer as we talked about and
that's also known as I mentioned as an
anonymous array. Now each box or slot
contains a value the array again has
five elements, but we're putting the
valley's into each box so each box has
got a different value so the box or
element index 0 contains number 10 the
box at array element 1 contains 35 and
so on. Alright, so let's see some of the
common errors that occur when working
with arrays, so here's an example of the
first common error accessing index out
of bounds, notice you can see we're
trying to assign the element at index 5
and that index doesn't exist in other
words it's out of bounds and the same
goes if you use a negative index arrays
can't have a negative index, so in other
words the number has to be 0 or higher
but note that an array can hold a
negative value, so in other words we can
put a negative number into one of those
boxes but we can't try and access a
specific box or element using a
negative number, but in this particular
case you can see that we have to find
the array on the first line int that the
right square brackets my array equals
then we've got the left and right curly
braces 10 35 20 17 and 18 separated by
commas and then semicolon on the end of
the line. So that's a five element array
but remembering that our array index
starts at index zero, so therefore my
element square brackets five in between
trying to set that the fifty-five is
giving us an error and you can see there
on the screen in the bottom right hand
corner and that's an error we're trying
to basically update a box that doesn't
exist in this particular array, so be
careful when accessing array elements
remember that array index positions
start from zero and go up to the length
take one in other words here we've got
five elements and the valid range is two
in to get access to those elements two
zero three four. Alright, so our second
common error happens when you loop
through an array, now there's a logic
error in other words we're starting here
from element one but remember that the
array index starts from zero, so if we
printed out the elements of the array
starting from index 1 the first element
which is at index zero won't be printed
in this scenario, so if this ever happens
to you just change the variable in the
for loop to start at zero, so you can see
what's happened in this code we've
assigned the values to the array of 10
35 20 17 and 18 but the output as you
can see has bypassed the value of 10 and
that's our the value from index 0 in the
array because the for loop as you can
see in the red box we started from the
value 1 and of course index position 1
contains the value of 35 so the last
common error that happens would again
cause an index out of range exception
now this is because the condition isn't
correct, so here we're using the less
than equal operator you can see that to
highlight it in the red box but we
should be using the less than operator
the problem happens because the last
iteration of the loop i variable will
have the value 5 the length of this
array is 5, so we've got 5 elements in
the array but the index range is from 0
through 4, so when i is equal to 5 the
condition will be true because we've
mentioned less than equal there
five is less than equal to five is true
there with five being the value of i and
five being the length of the array, so
this will cause an out of range
exception, so most importantly remember
that again that the index starts at zero
and ends at least one
so when looping it's easier to use the
less than sign than having to use the
length fake one, so here you can see that
we could change the codes of it i list
then my array dot length which would
work just fine. Alright so that's a recap
of arrays I hope you got a lot out of it
and we'll move on now to the next video.