I was learning how to sort arrays with different types of value holders. I tried to sort an array with Strings, but it comes up with the error message, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at SortingArrays.main(SortingArrays.java:50)
There aren't any errors the compiler found, but it comes up with this. The array with numbers worked fine, but the strings didn't. Here is my code.
import java.util.Arrays;
public class SortingArrays {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] aryNums;
aryNums = new int[6];
aryNums[0] = 7;
aryNums[1] = 89;
aryNums[2] = 45;
aryNums[3] = 234;
aryNums[4] = 2;
aryNums[5] = 75;
Arrays.sort(aryNums);
int i;
for (i = 0; i < aryNums.length; i++) {
System.out.println(aryNums[i]);
}
String[] aryStrings;
aryStrings = new String[5];
aryStrings[0] = "I have no idea what I'm doing.";
aryStrings[1] = "Should I know what I'm doing?";
aryStrings[2] = "I guess not.";
aryStrings[3] = "There's my boss.";
aryStrings[4] = "Whoop, he's looking. Hide!";
Arrays.sort(aryStrings);
int x;
for (x = 0; x < aryStrings.length; x++) {
System.out.println(aryStrings[i]);
}
}
}
iback to0.xbut indexing withi- which is why you should be declaring your indexers in the scope of theforloop instead of outside of it. This would then become a compiler error.