I have found several threads how to reverse an array. However, I don't follow why can't use the same code for both a static method and a void method.
Can someone explain the difference in the following examples?
For a static method we can write:
public static double[] reverse (double[] a)
{
int n = a.length;
for (int i = 0; i < n/2; i++)
{
double temp = a[i];
a[i] = a[n-(i+1)];
a[n-(i+1)] = temp;
}
return a;
}
But for a static void we have:
public static void reverse (double[] a)
{
for (int i = 0, j = a.length-1; i < j; i++, j--)
{
double temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}