0
import java.util.Arrays;
import java.lang.String;
public class Adding_elements_Array{
        static void add(int []arr,int element,int index,int size){
            for(int i=size-1;i>=index;i++){
                arr[i+1]=arr[i];
            }
            arr[index]=element; 
        }
    public static void main(String[] args){
        int []arr={1,2,3,4,5,6};
        int size=6;
        int element=7,index=2;
        add(arr,element,index,size);
        size+=1;
        System.out.println(Arrays.toString(arr));
    }
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
        at Adding_elements_Array.add(Adding_elements_Array.java:6)
        at Adding_elements_Array.main(Adding_elements_Array.java:14)

The above code is giving me this exception but the size of the array is 6 I tried many things but it is giving me the same exception I am using the same logic to code in C++ and the program in C++ is working but in java, it is not working. When I am giving the size 5 it is working but the program replaced 3 not moved it to the following index.

The output must be:
[1,2,7,3,4,5,6]
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
Novice
  • 1
  • 2
  • Yes, the array has size 6. Which means the elements are numbered from 0 to 5. There is no element 6 in a 6-element array. – Silvio Mayolo Jul 11 '22 at 04:44
  • Step through your code with the debugger. – tgdavies Jul 11 '22 at 04:48
  • Try an arraylist instead of a static array. Static arrays cannot change their size once created. I am surprised the equivalent code works in C - you're possibly overwriting the stack frame but the system has not detected it before the program finished. – cup Jul 11 '22 at 04:50
  • @cup static array do change their size I am practicing Dsa for many months in c and c++ I am using the same code and it works – Novice Jul 11 '22 at 04:52
  • But how this code is working in c++ then and not in java – Novice Jul 11 '22 at 04:55
  • C and (maybe) C++ don't inherently check for array overrun. It 'works' because the memory slot exists, but it's wrong because that memory slot is probably allocated to something else. You can't see the error because the values that are being overwritten, ```int size = 6```, is replaced by the same value, ```arr[6] = 6;``` – Immersive Jul 11 '22 at 05:11

1 Answers1

0

An array of size 6 has indices 0, 1, 2, 3, 4, and 5. An index of 6 is one past the end of the array. Since you're passing in 6 for size, i = size-1 sets i to 5, and then you index arr[i+1] which is arr[6], which is an invalid index.

Since you intend to increase the array size by 1, you probably just need to actually do that before the loop starts. According to How to add an element at the end of an array? , arrays in Java have a fixed length, so you probably want to use ArrayList instead. Then you can just call .add(0) to add an element to the end of the array, which will give your loop the space it needs to shift everything over.

John Haggerty
  • 218
  • 1
  • 8