Reputation: 1141
I am trying to insert an element at specific position but code is throwing an error of
java.lang.ArrayIndexOutOfBoundsException
I am new in java so can anybody help me to correct my code. I know doing something wrong in insertArr[i+1] = insertArr[i];
of insertPosition method. Any help will be count for good help.
My code
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] insertArr = {10,30,20,40,60,50,90,70,80};
Arrays.sort(insertArr);
int arrLength = insertArr.length;
System.out.println("At what position you want to insert");
int pos = in.nextInt();
System.out.println("What element you want to insert");
int key = in.nextInt();
insertPosition(insertArr,arrLength,pos,key);
for(int num : insertArr){
System.out.println(num);
}
}
public static int insertPosition(int insertArr[],int arrLength, int pos, int key){
if(pos > arrLength)
return arrLength;
for(int i=arrLength-1; i>=(pos-1); i--)
insertArr[i+1] = insertArr[i];
insertArr[pos-1] = key;
return arrLength;
}
Upvotes: 0
Views: 88
Reputation: 1369
First you must subtract 1 from the variable pos and verify if it is in the range of the length of the arrays.
Your code should look like this:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] insertArr = { 10, 30, 20, 40, 60, 50, 90, 70, 80 };
Arrays.sort(insertArr);
int arrLength = insertArr.length;
System.out.println("At what position you want to insert");
int pos = in.nextInt();
System.out.println("What element you want to insert");
int key = in.nextInt();
insertPosition(insertArr, arrLength, pos, key);
for (int num : insertArr) {
System.out.println(num);
}
}
public static int insertPosition(int insertArr[], int arrLength, int pos, int key) {
pos = pos - 1;
if (pos >= arrLength || pos < 0)
return arrLength;
for (int i = arrLength - 1; i >= pos; i--) {
insertArr[i] = insertArr[i];
}
insertArr[pos] = key;
return arrLength;
}
Upvotes: 0
Reputation: 11600
You need to ensure that i is less than insertArray length and greater than zero.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] insertArr = {10, 30, 20, 40, 60, 50, 90, 70, 80};
Arrays.sort(insertArr);
System.out.println("At what position you want to insert");
int pos = in.nextInt();
System.out.println("What element you want to insert");
int key = in.nextInt();
insertPosition(insertArr, pos, key);
Arrays.stream(insertArr).forEach(System.out::println);
}
public static void insertPosition(int insertArr[], int pos, int key) {
if (pos >= insertArr.length)
return;
for (int i = insertArr.length - 1; i > (pos - 1) && i < insertArr.length -1; i--)
insertArr[i + 1] = insertArr[i];
insertArr[pos - 1] = key;
}
In java length of an array is immutable, when you are calling for an elements from index that does not exist, you are getting this exception. In order to avoid that in for loops you are checking if i< array.length
or that i < array.length -2.
Secondly your method for insert can be simplified, like above.
Upvotes: 2
Reputation: 937
The problem is in this block of code:
for(int i=arrLength-1; i>=(pos-1); i--)
insertArr[i+1] = insertArr[i];
On the first iteration you're accessing insertArr[arrLength]
which exceeds the last index of array. So you should start iterating from i=arrLength-2;
Upvotes: 0
Reputation: 64
The length of an array is immutable in Java, You have to use a Collection of some sort such as a List/ArrayList
Upvotes: 0