这是我对这个问题的尝试,我如何将 0 移动到数组的末尾?我尝试将 0 元素与结束元素交换,那不是......
public void removeMiddle() {
int pos = values.length/2;
int n = values.length
if (n%2 == 0) {
int fMid = pos - 1;
values[pos] = 0;
values fMid = 0;
} else {
int j = n-1;
int k = j/2;
int l = k+1;
int m = n-l;
values[m] = 0;
}
}
Array = {5, 2, 7, 9, 1, 3, 2, 4}
result = [5, 2, 7, 0, 0, 3, 2, 4]
Expected: [5, 2, 7, 3, 2, 4, 0, 0]
Array = {5, 2, 7, 9, 1, 3, 2}
result = [5, 2, 7, 0, 1, 3, 2]
Expected: [5, 2, 7, 1, 3, 2, 0]