Looked around, couldn't find any similar questions in java..
Basically I need to add a number to an int array in a specific position index
I can only use Arrays, no ArrayLists
Here is what I have so far, and I know why it doesn't work, but I can't figure out how to fix that problem of overwriting, which I don't want it to do.
The task is a non-overwriting insert. e.g. the final result would be
[1 2 1337 3 4 5 6 7 8]
Here is the code snippet:
public void main(String[] args)
{
int[] array = {1,2,3,4,5,6,7,8};
array = add(array, 2, 1337);
for(int i : array)
System.out.print(i + " ");
}
public int[] add(int[] myArray, int pos, int n)
{
for (int i = pos; i<myArray.length-1; i++){
myArray[i] = myArray[i+1];
}
myArray[pos] = n;
return myArray;
}