I am trying to write a method that takes an array, and adds adjacent cells together to produce a new array. It's easier to explain with examples:
{11, 4, 3, 18, 23, 9} --> {15, 21, 32}
{5, 5, 21, 13, 1} --> {10, 34} (the sixth cell is just ignored)
public static int[] collapse(int[] a) {
int[] b = new int[a.length / 2];
for (int j = 0 ; j < (a.length - 1) ; j+2) { //goes thru original array
for (int i = 0 ; i < b.length ; i++) { //populates new array
b[i] = a[j] + a[j+1];
}
}
return b;
}
I figured this required using nested for-loops, the first one to go thru the original array, and the second one to populate the new array. I know that the j+2 in the first for-loop is syntactically incorrect, but I cannot figure out another way to accomplish the same idea.