This is the 17th day of my participation in the August Challenge

Are you friend of IndexOutOfBoundsException familiar often encountered, in fact, we can also meet his subclasses ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException So why does it run out of this anomaly? An index of some kind (such as an array, string, or vector) is out of range.

IndexOutOfBoundsException

Let’s take an example of one of the most common things that goes wrong: error repetition

public static void main(String[] args) {
    List<String> x = new ArrayList<>();
    x.add("0");
    x.add("1");
    int y = 2;
    x.get(y);
}
Copy the code

The results

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
Copy the code

Why is this exception thrown? Let’s look at the source code.

public E get(int index) {
    rangeCheck(index);

    return elementData(index);
}

private void rangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
Copy the code

You’ve seen the checks the source code does when fetching elements, and we’re going to do the same when fetching elements.

public static void main(String[] args) {
    List<String> x = new ArrayList<>();
    x.add("0");
    x.add("1");
    int y = 2;
    if(x.size() > y) { System.out.println(x.get(y)); }}Copy the code

So you can avoid this problem. This is just an example, and there are many more complicated ones in the real world, but each one certainly has its own solution.

ArrayIndexOutOfBoundsException

About ArrayIndexOutOfBoundsException abnormalities, see how the prefix is an Array of abnormal. Let’s take one of the most common examples of errors that are duplicated

public static void main(String[] args) {
    int[] x = {1.2};
    int y = 2;
    System.out.println(x[y]);
}
Copy the code

The results

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
Copy the code

The solution is the same

public static void main(String[] args) {
    int[] x = {1.2};
    int y = 2;
    if(x.length > y){ System.out.println(x[y]); }}Copy the code

StringIndexOutOfBoundsException

Ha ha, this is needless to say, must be related to String. One of the most common examples is error repetition

public static void main(String[] args) {
    String x = "Hello";
    System.out.println(x.substring(3));
}
Copy the code

The reason is that the intercepting character is out of bounds under the string array table.

A simple question

The following code throws an exception.

public static void main(String[] args) {
    List<String> x = Arrays.asList("0"."1");
    int y = 2;
    System.out.println(x.get(y));
}
Copy the code

conclusion

About all of us can avoid IndexOutOfBoundsException the exception, we are operating strings, arrays, collections, to carefully make a judgment, we won’t to appear this BUG that combing the code again. There are a lot of things that can cause this error, so what we need to do is to be careful not to have to change the code again just because we missed a little judgment.