In my code i am comparing the 2 elements of an array. but i got the following exception.please can anybody help me
array:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Sarray.main(Sarray.java:64)
public class Sarray
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print( "Enter sorted array length:" );
int length = scan.nextInt();
int[] a = new int[length];
System.out.println("Enter integer sorted array:");
for(int i = 0;i<length;i++)
{
String token = scan.next();
a[i] = Integer.parseInt(token);
}
System.out.print("Unique array:");
int[] b=new int[length];
int k=0;
for(int i=0;i<length;i++)
{
//here i got Exception
if(a[i] != a[i+1])
{
b[k++]= a[i];
}
}
for(int i=0;i<k;i++)
{
System.out.print(b[i]+" ");
}
}
}