Passing array to method in Java

Java 8Object Oriented ProgrammingProgramming

Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array -

Example

public static void printArray(int[] array) {
   for (int i = 0; i < array.length; i++) {
      System.out.print(array[i] + " ");
   }
}

You can invoke it by passing an array. For example, the following statement invokes the print Array method to display 3, 1, 2, 6, 4, and 2 -

printArray(new int[]{3, 1, 2, 6, 4, 2});
raja
Published on 07-Feb-2018 05:45:24
Advertisements