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: 2020-02-25T05:21:22+05:30

246 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements