Read Also: [Solved] java.util.MissingFormatArgumentException in Java
What is Index?
The index is the representation of the position of an element inside an array, a list, or other data structure that has order. Indexes in Java are 0 based. It means the 0th index represents the position of the 1st element, the 1st index will represent the 2nd element, the 2nd index will represent the 3rd element, and so on.[Fixed] java.lang.IndexOutOfBoundsException
First, we will produce the java.lang.IndexOutOfBoundsException before moving on to the solution. import java.util.List;
import java.util.ArrayList;
public class IndexOutOfBoundsExceptionExample {
public static void main(String args[]) {
List<String> list = new ArrayList<>();
list.add("Alive");
list.add("is");
list.add("Awesome");
list.get(5);
}
}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 3
Explanation
We have added three elements to the list. "Alive" is the 1st element at index 0. "is" is the second element at index 1. "Awesome" is the third element at index 2.
When we are executing
list.get(5)
We are trying to fetch the element at index 5. But we have elements stored till index 2. Hence, we get the IndexOutOfBoundsException.
Solution
We can easily solve this exception by adding an if block condition that the index should always be smaller than the list size as shown below in the example:
if (index >= 0 && index < list.size())
list.get(index);
Subclasses of IndexOutOfBoundsException
There are two subclasses of IndexOutOfBoundsException:1. ArrayIndexOutOfBoundsException
2. StringIndexOutOfBoundsException
1. ArrayIndexOutOfBoundsException
According to Oracle docs, ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException. This exception is thrown to indicate that an array has been accessed with an illegal index. The illegal index means
a. If the index is negative or
b. If the index is greater than or equal to the size of the array
Producing the ArrayIndexOutOfBoundsException
Example1: When the index is negative
public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String args[]) {
int[] arr = {2, 5, 29};
// Passing negative index
int exception1 = arr[-1];
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 3
Example2: When the index is greater than or equal to the size of the array
public class ArrayIndexOutOfBoundsExceptionExample2 {
public static void main(String args[]) {
int[] arr = {2, 5, 29};
// Passing index greater than the size of the array
int exception2 = arr[8];
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 3
Solution
We can easily solve the ArrayIndexOutOfBoundsException by adding an if block condition that the index should always be smaller than the array size as shown below in the example:
public class ArrayIndexOutOfBoundsExceptionSolution {
public static void main(String args[]) {
int[] arr = {2, 5, 29};
int num = 0;
int index = 5;
// if condition check to solve ArrayIndexOutOfBoundsException
if (index >= 0 && index < arr.length)
num = arr[index];
else
throw new ArrayIndexOutOfBoundsException("Invalid Index");
System.out.println(num);
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Invalid Index
2. StringIndexOutOfBoundsException
According to Oracle docs, StringIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException. This exception is thrown by String methods to indicate that an index is either negative or greater than the size of the String. For some methods like the charAt() method, this exception is also thrown when the index is equal to the size of the string.
Producing the StringIndexOutOfBoundsException
Example1: When the index is negative
public class StringIndexOutOfBoundsExceptionExample {
public static void main(String args[]) {
String str = "Love Yourself"; // length = 13
// Passing index having negative value
char exception1 = str.charAt(-4);
}
}
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -4
Example2: When the index is greater than or equal to the size of the String
public class StringIndexOutOfBoundsExceptionExample2 {
public static void main(String args[]) {
String str = "Alive is Awesome"; // length = 16
// Passing index greater than the size of the string
char exception1 = str.charAt(23);
}
}
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 23
Solution
We can easily solve the StringIndexOutOfBoundsException by adding an if block condition that the index should always be smaller than the String size as shown below in the example:
public class StringIndexOutOfBoundsExceptionSolution {
public static void main(String args[]) {
char ch = Character.MIN_VALUE;
int index = 22;
String str = "Love Yourself"; // length = 13
// if condition check to solve StringIndexOutOfBoundsException
if (index >= 0 && index < str.length())
ch = str.charAt(index);
else
throw new StringIndexOutOfBoundsException("Invalid Index");
System.out.println(ch);
}
}
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Invalid Index
That's all for today, please mention in the comments in case you have any questions related to java.lang.IndexOutOfBoundsException in Java.
0 Commentaires