The first value written into your array is 0 because you initialize i
with 0 in the for loop.
Therefore your loop will only insert the values 0 - 6.
Change this initialisation to i = 1
and in addition you also need to change the condition of the for loop to arr.length + 1
or i <= arr.length
, so it will count up to 7.
Integer[] arr = new Integer[7];
for (int i = 1; i < arr.length + 1; i++) {
arr[i] = i;
}
What you also can do instead of changing the loop itself, is to change the loop body. Just add 1 to i when assigning it to arr[i]:
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
In this case i will count from 0 to 6, and assign 1 to 7 to your array