Returning an Array from a Method in Java

Java 8Object Oriented ProgrammingProgramming

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;
}
raja
Published on 07-Feb-2018 10:47:37
Advertisements