How to add an integer to an array in Java?

To add an integer to an array in Java, do this: array[index] = element.

Here's how you do it:

int[] array = new int[5];

array[1] = 50;
array[4] = 100;

// [0, 50, 0, 0, 100]
System.out.println(java.util.Arrays.toString(array));