0

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]);
        }

    }

}
2
  • 1
    You never reset i back to 0. Commented Dec 23, 2015 at 14:25
  • 2
    Specifically, you're looping with x but indexing with i - which is why you should be declaring your indexers in the scope of the for loop instead of outside of it. This would then become a compiler error. Commented Dec 23, 2015 at 14:26

7 Answers 7

4

You are getting runtime exception. ArrayIndexOutOfBoundsException means you are trying to access array beyond its capacity with what its defined.

Problem is with this code:

for (x = 0; x < aryStrings.length; x++){
    System.out.println(aryStrings[i]);
                                  ^^
}

You are using i as index instead of x. Here i (after your print statements) would be 6 in your case and your array can hold 5 elements which start with 0 and ends with 5.

Sign up to request clarification or add additional context in comments.

Comments

3

At the very end of your program:

 System.out.println(aryStrings[i]);

i = 6, of course it out of bounds

What you need is:

System.out.println(aryStrings[x]);

Comments

2

i after your first loop is 6.

so here

for (x = 0; x < aryStrings.length; x++){
        System.out.println(aryStrings[i]); // i here is 6 but there are 5 elements in aryStrings
    }

use x in this loop not i

Comments

1

As other commenters have said, the problem is with your iterating variable. But to completely avoid this problem, use a for-each loop. As Joshua Bloch says in Effective Java, 2nd Edition (Item 46):

// The preferred idiom for iterating over collections and arrays
for (int aryNum : aryNums) {
   System.out.println(aryNum);
}

Or better in Java 8:

Arrays.stream(aryNums).forEach(System.out::println);

Comments

0

You're using i instead of x in the second print statement

Comments

0

Change the index in x

   System.out.println(aryStrings[x]);

Comments

0

As the other answers have stated, by the time you reach your second loop, i is equal to a higher integer than your String array has indices.

Another way to fix your issue is to reset the value of i:

int x;
i = 0;//re-initialize    

for (x = 0; x < aryStrings.length; x++) {
    System.out.println(aryStrings[i]);
}

However, this defeats the purpose of your x variable.

As a rule of thumb, I'd keep your incrementer local to your loop to prevent problems like this arising:

//declare x in the loop
for (int x = 0; x < aryStrings.length; x++) {
    System.out.println(aryStrings[x]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.