Passing, Returning An Array To, From a Method in Java Programming

In this tutorial, you will learn how to pass / return an array to / from a method in java programming.

You will learn how to pass an array to a method and work with in inthe method and then how you can return an array from the method using the return statement in java language in detail with example.

Source code for this video tutorial


package methods;

public class Tutorials {
 public static void main(String[] args) {
  int[] marks = {22,66,33,99,88,77};
  display(marks);
  
 // int[] revmarks;
 // revmarks = reverseArray(marks);
  System.out.println("Reversed array elements");
 // display(revmarks);
  display(reverseArray(marks));
  
 }
 
 public static void display(int[] input){  
  for(int counter = 0; counter < input.length; counter++){
   System.out.println(input[counter]);
  }  
 }
 
 public static int[] reverseArray(int[] input){
  int[] reverse = new int[input.length];
  for(int i = 0,j = reverse.length - 1; i < input.length; i++,j--){
   reverse[j] = input[i];
  }
  return reverse;
 }
}
Back To Top