IndexOutOfBoundsException

IndexOutOfBoundsException



public class IndexOutOfBoundsException extends RuntimeException

It comes with two subclasses used very often in coding with arrays and strings.

1.      ArrayIndexOutOfBoundsException
2.      StringIndexOutOfBoundsException


IndexOutOfBoundsException: check the index number entered by the user is within the range of an array or string.

List<String> list1 = new ArrayList<String>(5);
for(int i=0;i<10;i++){
      list1.add(6,String.valueOf(i));}

ArrayIndexOutOfBoundsException: When Trying to get or put value from index which is the bigger than size of array

String[] myArray = new String[3];
myArray[4] = "ratik";
System.out.println("myArray..." + myArray[4].toString());

StringIndexOutOfBoundsException: When we try to retrieve the character at a specific index, but the specified index is much larger than the length of the string itself

String str = "Java Code Geeks!";
System.out.println("Length: " + str.length());
char ch = str.charAt(12);




Comments

Popular posts from this blog

Install Alfresco Content Service 6.0 on ubuntu 16 using distribution zip

Lucene and fts-search

Call javascript webscript from contoller