So I cant for the life of me figure out why I am getting this OutOfBound exception and why the program wont work.
public class test { public static void main(String[] args) { int[] array = {58,24,13,15,63,9,8,81,1,78}; Out.println(java.util.Arrays.toString(array)); int[] firstArray= new int[array.length/2]; for(int i = 0; i<array.length/2; i++) { firstArray[i] = array[i]; } Out.println(java.util.Arrays.toString(firstArray)); int[] secondArray = new int[array.length/2]; for(int i = array.length/2; i<array.length-1; i++) { secondArray[i] = array[i]; } Out.println(java.util.Arrays.toString(secondArray)); } }
So my goal here is to split this inital array in half and store both of the halves in new arrays.The first half works but the second I cant get to work. When I try to run the program it gives me "Index 5 out of bound for length 5 exception". Now the way I am seeing this is that the second array has the length of 5 and in the for loop we are starting from the 5th position going to the last element in the original array, and copying the values. Why am I getting this error?
PS.I have also tried going backwards, trying to go from the last unit of the array to the middle and i get the same error