oh it's time for another challenge
this is the reverse challenge so firstly
write a method called reverse that's got
an int array as a parameter now the
method shouldn't return any value in
other words the method is allowed to
modify the array parameter so in main
test the reverse method and print the
array both reversed and non reversed so
to reverse the array you have to swap
the elements so that the first element
is swapped with the last element and so
on so for example if the array contains
1 2 3 4 5 then the reversed array would
be 5 4 3 2 1
and create a new console project with
the lam reverse array challenge for this
challenge alright so that's it see if
you can figure it out pause the video
now and try your hand at this challenge
and once you've completed come back to
see my solution so pause the video now
and I'll see you when you get back okay
so welcome back
hopefully managed to finish that so
let's go through and solve this
particular challenge so what we need to
do is start off by writing the method
reverse so let's go ahead and do that
let's go ahead and type private static
void reverse then in parentheses int
lifting right square brackets array then
I left to right curly braces as per
normal so within this method we're going
to use a for loop to go through the
array elements the tricky part is the
condition needed for the loop so inside
the loop we have to swap the elements as
in the first element we'll swap it at
the last the second with this V next to
last and so on now later in this video
I'll provide some images demonstrating
this swapping pattern so before running
the loop let's declare an int variable
so int max index you're going to set
that to the maximum value that an index
can use this variable will represent the
maximum index that we can use now we
already know that the index ranges from
0 to the length of the array minus 1 so
that will be the value that we'll store
in here so we set that to Ray dot length
minus 1
now I stated this max index variable
represents the maximum index our next
variable will be called a half-length
which will represent half of the array
length now this is where it becomes
tricky we need to loop through the array
and continue swapping until we get to
the halfway point of the array so let's
declare that variable now that's going
to be int half length is equal to a R a
dot length divided by two now you will
be wondering here Tim what are we only
looped through until we reach the
halfway point of the array well the
reason is with swapping elements so once
we reach halfway the second half has
already been swapped with the first so
the array is now fully reversed at that
point now the image that I'll bring up
later in this video will show you and
hopefully make that more clearer all
right so next let's start generating the
code for our for loop so the next time
I'm going to type 4 in parentheses int I
equals 0 I less than 1/2 length
semicolon then an I plus plus an open
the code block there now inside the loop
we want to swap two elements first to do
this we need to create a temporary int
variable which I'm going to call temp so
the top in int temp equals array I in
square bracket so we're saving the
current element position or element I as
we're cycling through the array so
basically since I like the I variable
represents index starting from position
0 in the first iteration we have to swap
the element at index 0 which is our
first element with the index at the Max
index which is our last element so to
accomplish this we want to type array
and in square brackets I is equal to
array swear brackets again it's going to
be max index minus I and closing off the
square brackets and semicolon to
underline now we're our temp variable we
save the element that was at index I
then on the next line we moved the
element from index max index take I to
index I so
now all we have to do is move the first
element to the last index which is where
our temp variable comes into play so we
can know type Perret and square brackets
max index minus I and square brackets
there
equals temp so we're putting that value
in there because the element originally
had index I was saved to the temp
variable on line 14 now that's all the
code we need to swap the elements
it might look strange at first but
basically as I progresses from zero to
the index of half of the length each
element swaps with its mirror element
meaning the first with the last the
second with the next to last and so on
but we no need to test this out to
confirm that it does work
so let's write some code in the main
method to do just that all right so I'm
going to start by creating an array we
can use for testing purposes so I'm
going to type int left to right square
brackets array is equal to then we're
going to put curly braces there to
initialize it to 1 5 3 7 11 9 15 so
there's our array you'll obviously
that's not ordered at the moment so now
that we've got this array we need to
reverse it but before we do that we
should print it out since the reverse
method is modifying the array parameter
so let's actually try printing this out
first so we'll print out array is equal
to plus arrays dot two string passing
our array to that document all right so
after we've printed out our unmodified
array let's call the method reverse and
then we'll print out the array again so
on the next line i'm going to type
reverse alright we're passing our array
as an argument then let's just print out
the next line we're gonna put reversed
array is equal to plus arrays dot two
string again and the same array that's
an argument all right so let's now run
the application to test out our code to
see if it works
all right so you can see first my
original rate 1 5 3 7 11 9 and 15 with
the elements then then the next line
we've got the reverse version 15 9 11 7
3 5 and 1 that means that we've achieved
the desired result of reversing the
array or the elements I should say in
the array now just to see what happens
let's go back to our reverse method and
let's actually remove this divided by 2
component and remove that to show you
what happens if we don't divide the
array length by 2 you're right so I'm
gonna run it again and you can see in
this case we're now not getting the
correct result since the elements
continue to swap until the end of the
array even though the original first
element correctly swapped with the last
when we reach the last element and then
swaps back into the first position so in
this case we swapped 1 and 15 as the
first iteration and swapped them again
in the last iteration as a result both
lines in the console have the same
elements at first glance it might look
like our code is not working at all but
it actually is working we just happened
to swap all the elements back to their
initial position again so let's revert
that change and run it again you can see
we've now got the correct result again
so let's a standard earth earlier we're
not going to review some images showing
the execution steps of the for loop and
the functionality of the right of the
array just to hopefully clarify any
doubts you might have on how it's
working now for our example we're going
to use an array with the elements 1 2 3
4 5 you know for the first iteration
that variable I equals 0 and if we look
at the lower left box here we could see
what this code would look like in the
for loop as you can see we're storing
the element that is at index 0 in our
temp variable so in this case the
element at index 0 has a value of 1 next
we set the element at index 0 to a value
of 5 since that is the value in the last
index index 4 finally the last element
is set to the value of temp which is 1
and which is also the original value at
index 0 now these
tips are executed during each loop
iteration on the right hand side of this
image there's an illustration showing
how the array transforms when in the
line int temp equals array left square
bracket I right square bracket is
executed we're storing the first element
in the temp variable the next line sets
the element at index 0 to the value of
the last element in other words 5 is now
an index 0 and the last line changes the
last index to a value of 1 since temp
has a value of 1 the neech iteration of
the loop follows this process
now after the first iteration is
complete the array is now 5 2 3 4 1 so
let's now move on to the next iteration
now in this iteration I equals 1 and our
array reads is 5 2 3 4 1 due to the
processing in the first iteration so
this time temp will be the element at
index 1 and the logic will follow that
of the first iteration so to sum up the
element at index 1 swaps with the
element at index 3 in our arrays can
tree 5 4 3 2 1
meaning it's now fully reversed the I
variable will still increment and become
2 but the for loop will stop since half
length equals 2 so in other words in our
loop condition we now have 2 less than 2
since that condition is false the loop
won't continue and as demonstrated
before if we continue looping through
the whole array while following the same
logic then the elements will actually be
moved back to their original index and
the array in fact wouldn't have been
reversed all right so that's it for the
challenge I hope the looping logic made
sense and I look forward to seeing you
in the next video