declare

Passing, Returning Arrays To / from Methods in Java Programming Video Tutorial



Sharing buttons:

This is Anil from Learning Lad education and welcome to another tutorial on Java programming

language.

In the previous tutorials we have learnt about the methods in Java and also I have taught

you guys how exactly we can pass parameters to the methods and here in this tutorial we

are gonna learn how exactly we can pass an array to a method and also how exactly we

can return an array from a method.

So just for the demonstration purpose in this tutorial what we are gonna do is, inside my

main method i'm gonna create an Array.

so I just gonna create an integer array and lemme call it as marks and inside this array

i'm gonna store some integer values and lets say 22 66 33 99 88 77.

now what we are gonna do is we are gonna create a method and we are gonna pass this marks

array as the argument to that method.

so after this Main method inside my tutorial class, i'm gonna create a method.

so first we need to write the modifiers.

So its gonna be Public Static and after this modifier part, we need to write the return

type.

Here i'm gonna assume that my method is gonna return void type.

It means no return values.

so I just gonna use void, and then we are gonna write the method name.

The method that I'm gonna create here is gonna take an integer array as a parameter and its gonna

display the elements of that array.

so I just gonna call this method as display, and then a pair of parenthesis and then a pair

of curly braces.

now between these parentheses, we need to specify the parameters taken by this display Method.

here our method is gonna take an integer array.

so I have to declare an integer array here.

so I just gonna use the data type as int, then a pair of square brackets and then I just

need to give a name for the integer array.

so I just gonna call it as input.

now our display mythos is not gonna return any value and its gonna take an integer array

as a parameter.

so inside this display method, we are gonna display the elements of this input array.

so I'm gonna use a for loop.

so its gonna be for and we are gonna need a loop counter variable and i'm gonna declare

that right here and i'm gonna initialise that one to 0 and then we need to write the condition

part.

So its gonna ne counter less than, we need to loop through all the elements of this input

array.

so we can get this length of the input array by using the length property.

so Its gonna be input, the array name dot operator and then the length and then we are

gonna have the semicolon and we need to increment the value of the counter variable.

And after that we are just gonna use the print line Method.

So its gonna be system dot out dot printline and here we are gonna refer the input

array and we are gonna refer the the element position by using the counter variables value.

so this display method now, its gonna take an array as a parameter an integer array as

a parameter and its gonna displays the elements of that integer array.

So now we are gonna call this display Method from our main Method.

so here I'm gonna write display, the method name and then I have to pass this marks array.

so I just gonna write marks here as the argument and i'm gonna save this program and i'm gonna

run this.

And now you guys can see here, the elements of that array 22 66 33 99 88 and 77, are successfully

passed to this display method and we have successfully printed the element of the passed

array.

so this is how you guys can pass a single dimensional array to a method.

The next thing that I want you guys to teach you is,how exactly we can return an array from a method.

So here in my tutorials class, i'm gonna define another method.

which is going to take an integer array as a parameter and also its gonna return an

integer array.

The method that i'm gonna create is gonna take an integer array and its gonna create

another array with reversed element of the passes array and its gonna return that reversed

array.

so we are gonna write the modifier part and its gonna be public and then we are gonna

have the static keyword and after that we need to write the return type.

here our method is gonna return an integer array.

all of you know that if you want to return an integer value from a method, you guys need

to use the keyword int. here in this case we are returning an integer

array.

so we are gonna use the square brackets to specify that we are talking about the arrays

here. and then we just need to give a name for our

Method and here our method is gonna reverse the array element.

so I just gonna say reverse array. and then we need to have a pair of parenthesis and

then a pair of curly braces.

now as a parameter to this reverse array, we are gonna pass an integer array.

so All of you know how you can do that.

just now I have taught you about that.

So its gonna be int and I'm gonna call this array as input.

All right.

now what we are gonna do is we are gonna can create another array inside this reverse array

method.

because we don't want to reverse the element in this input array.

we want to reverse the elements and then we want to store it in another array, and we

want to return that array.

so what we gonna do is we can create an integer array.

so our array is gonna be of type integers.

So i'm gonna declare it.

So its gonna be here int and then the other name and then i'm gonna say reverse and and

then we are gonna write new you know the new keyword to create an Array and then we are

gonna have the data type which is gonna be int and now here between the square brackets,

we need to specify the length for this reverse array.

now this reverse array is gonna contain the element of this input array in reverse order.

So the length of this reverse array is gonna be equal to the length of this input array

and I all of you know that we can get the length of this input array by using the length

property.

so I'm gonna write input here then the array name and then i'm gonna use the dot operator

and then I'm gonna use the length property and the next thing that we are gonna do is

we are gonna loop through all the elements of this input array and we are gonna store

that and reverse order.

For that purpose i'm gonna use a for loop and inside this for loop we are gonna use

two variable.

one for the input array and one for the reverse array.

so I'm gonna create a variable called i and this variable is gonna be for the input array

and that's why I'm gonna initialize that one to a value of zero.

And then we are gonna need another variable for this reverse array and that variable is

also going to be of type integers and I'm gonna call it as j.

here we are gonna initialise this j variable with one less than the length of this input

array because in an array the last element will be stored at the position One Less than

the length of that array.

so here what we are gonna do is, first we are gonna take the first element from the

input array and we are gonna store that in last element of the reverse array.

so here I'm gonna use reverse dot length minus one.

so our i is going to be equal to zero which begin with the first location of the input

array and this j is gonna contain a value which will indicate the last position of this

riverse array. and then we need to write a condition.

we want to run this loop until we loop through all the elements.

so we are gonna use the condition as i less than input dot length and then we need to

write the updation part.

here we want to increment the value of i.

So its gonna be i++ and then i'm gonna use the comma operator and we need to decrement

the value of j.

so its gonna be j--.

so here you guys can see inside is for loop we are using more than one variable.

so here we have separated that using the comma operator.

in this the initialisation part and also in this updation part you guys can see that.

in this condition part if you want to use the comma operator and if you want to form

a complex condition then you have to use a logical operators.

but in this case does not necessary because we want to run this loop until we loop through

all the elements of this input array and that's why this one condition using this variable

I is enough.

And now inside is for loop what we are gonna do is we are gonna store the elements in this

reverse array.

so its gonna be reverse of j equal to input of i.

so now after this for loop, this reverse array is gonna contain all the elements of this

input array in reverse order.

The last in that we are gonna do is, we gonna return this reverse array.

so after this for loop, before the end of this reverse array method, we are gonna return the

array and to return a value we need to use the return keyword and here we are gonna specify

reverse.

that's it.

now our reverse array method is gonna take an integer array as a parameter and its gonna

reverse that passed away and its gonna return a new array with reversed elements.

so now what we are gonna do is we are gonna call this reverse array method in our main

method and we are gonna pass an array to that and here now our reverse array method is gonna

return an integer array.

so to store that what we are gonna do in this main method is we are gonna create an integer

array.

so its gonna be int and then it's going to be an array.

so I just gonna declare an array and i'm gonna call it as revmarks. and here make a note

that we are not allocating the memory for This revmark.

The array returned by this riverseArray method will be stored in this revmarks variable.

so here what we are gonna do is we are gonna write revmarks equal to then we are gonna

call this riverse array method.

so I just gonna copy this and paste it here. and now to this reverse array method, we need

to pass an array.

so i'm gonna pass this marks array and after that what we are gonna do is, we are gonna

use a printline statement just to make our output pretty.

you know to differentiate between this marks array and this reverse array element.

So i just gonna use system.out.printline method and i'm gonna say reversed array elements

and then we are gonna call this display method again.

so its gonna be display and now we are gonna pass This revmark array.

so I just gonna copy this and paste it here and then I'm gonna save this and I'm gonna run

this program.

Now you guys can see, this is the array, Marks array which we have printed out using the

display method and then here is the reversed away from our reverse array method.

so you guys can clearly see here, array elements are been reversed.

so 77 88 99 33 66 and 22.

here and in this main method what we have done is we have declared this revmarks to

store the return value from this reverse array method.

what we can do is we can directly call this display method and we can pass the return

value, you know the returned array to this display method directly.

so what I'm gonna do is i'm gonna comment in this code and this one also and we are

gonna comment in this one too.

so that when you get the source code of this tutorial you guys can try it out.

And here I'm gonna call the display Method again and here what we are gonna do is we

are gonna pass the return value from this reverse array method, directly to this display

method.

so I'm gonna copy this and i'm gonna paste it here and if I save this and run this program,

its gonna work just like the way it was working before.

so i'm gonna run this.

you guys can see there is no difference in the previous Run.

so this is how you guys can pass an array, a single dimensional array to a method and

also you guys can return a single dimensional array from a method.

so thank you for watching and Don't forget to subscribe to my channel.

you guys can like my Facebook page @ facebook.com/learningLad and also you guys can follow me on Twitter

@ learningladEdu and also you guys can get the source code of this tutorial in my

website learninglad.com and I'll see you next tutorial.