I am trying to print every number that has an index above the median. However, i keep getting this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Test.main(Test.java:10)
Here is my code:
public class Test {
public static void main(String[] args) {
int list[] = {1,2,3,4,5};
int list1[] = new int[2];
int start = (int)(Math.ceil(list.length/2));
String output = "";
for(int i = start; i < list.length; i++) {
int indexB = (i - 3);
list1[indexB] = list[i]; //The error is concerned about this line of code
}
for(int j = 0; j < list1.length; j++) {
output += list1[j] + " ";
}
System.out.print(output);
}
}