I want to invert an array, such that when I enter: 1 4 5 6
the software returns: 6 5 4 1
This is what I've written, however, I keep receiving an arrayIndexOutOfBoundException.
import java.io.*;
import java.util.*;
public class Oef9
{
public int[] array_oef9 (int[] array1){
int lengte=array1.length;
int[] array= new int[lengte];
for (int i=0; i<lengte;i++ ){
array[lengte]=array1[i];
lengte=lengte-1;
}
return array;
}
}
I discovered this solution online that claimed "don't update lengte in the loop, because it's also utilizing in the loop condition, or else I and lengte would "meet" halfway through the iteration." Is that correct? Can somebody assist me?