Returning an Array from a Method in Java


A method may also return an array. For example, the following method returns an array that is the reversal of another array -

Example

public static int[] reverse(int[] list) {
   int[] result = new int[list.length];
   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
      result[j] = list[i];
   }
   return result;
}

Updated on: 25-Feb-2020

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements