I have this code to swap pairs of array elements:
int[] a= new int[]{1,2,3,4};
for(int i=0; i<a.length ;i++)
{
int temp= a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
However, I am getting the following exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at com.B.main(B.java:14)
Why am I getting this exception? How can I fix it?
ArrayIndexOutOfBoundsException1) From for loop conditioni < a.lengthwhile accessing a[i] 2) when conditioni < a.length -1while accessing a[i+1)