Q:

Overwrite an Array

I am new at Java and I am trying to overwrite an array like the following: {2,7,6,1,9} to {9,13,7,10,9}. So I am trying to add up neighbouring numbers in an array and overwrite the first number with the sum. My code looks like this:
int add = numbers[i] + numbers[i+1];
int[] numbersAdd = new int[numbers.length];
i = 1;
int j = 1;
while(j < numbers.length) {
    numbersAdd[j] = add;
    i++;
    j++;
}

I get always an IndexOutofBoundException.
Can you help me with that? Thank you!

A:

You should move add inside the while loop, and change the starting and ending indexes of i and j, like:
int[] numbersAdd = new int[numbers.length];
int i = 0;
int j = 0;
while(j < numbers.length - 1) {
    int add = numbers[i] + numbers[i+1];
    numbersAdd[j] = add;
    i++;
    j++;
}
numbersAdd[numbers.length - 1] = numbers[numbers.length - 1];

or it can be done using a for-loop:
int[] numbersAdd = new int[numbers.length];
for(int i = 0; i < numbers.length - 1; i++) {
    numbersAdd[i] = numbers[i] + numbers[i + 1];
}
numbersAdd[numbers.length - 1] = numbers[numbers.length - 1];

