ArrayIndexoutOfBound error

what happen if you insert more element than array size, it will
give error called ArrayIndexoutOfBound which is run time Exception.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
package com.spy.javaforlearn;
 
public class ArrayIndexoutOfBound 
{
 
	public static void main(String[] args) 
	{
 
		// Declaring int Array of size 3
 
		int[] number=new int[3];
 
 
		//getting size of Array
 
	     System.out.println("Size of Array: "+number.length); 
 
	    //initializing Array
 
	     number[0]=10;
	     number[1]=15;
	     number[2]=20;
	     number[3]=45;//Array size is 3 but we insert 4 element
 
	     //accessing array element
 
	     System.out.println("accessing array element");
 
	     for(int i=0;i<number.length;i++)
	      {
	    	 System.out.println(number[i]);	 
	      }
 
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *