For this answer I assume that your ARRAY has a blank box (free position) to spare for the new element. Then it will be very easy.
System.arraycopy(yourArray, 0, yourArray, 1, yourArray.length - 1);
yourArray[0] = newElement;
You don't need a temp array if yourArray can have an additional element. arraycopy() can do all the hardwork of creating temp array, copying values, etc. for you. Make sure BOTH srcPos and destPos are same which is yourArray
Here is the full demo:
public static void main(String[] args) {
int[] yourArray = new int[5];
Arrays.fill(yourArray, 0, 4, 1);
System.out.println("Assume your array looks like this (with additional blank box for new element): " + Arrays.toString(yourArray));
int newElement = 5000;
System.arraycopy(yourArray, 0, yourArray, 1, yourArray.length - 1);
yourArray[0] = newElement;
System.out.println(Arrays.toString(yourArray));
}
If in case you DON'T have any extra space in your array already, you need another array as a placeholder with extra space (finalArray). Instead of having same array in both places, just change destPos to finalArray.
Here is the demo for that:
public static void main(String[] args) {
int[] yourArray = new int[5];
Arrays.fill(yourArray, 0, 5, 1);
System.out.println("Assume your array looks like this (withOUT additional blank box for new element): " + Arrays.toString(yourArray));
int newElement = 5000;
int[] finalArray = new int[yourArray.length + 1];
System.arraycopy(yourArray, 0, finalArray, 1, yourArray.length);
finalArray[0] = newElement;
System.out.println(Arrays.toString(finalArray));
}
ArrayList. Using an array of integers is not nearly as convenient as ArrayList<Integer>.b[0]=4this way you can add value at first