我有以下代码应该通过将元素复制到新数组并跳过其他数组来缩短数组。但是,我不断收到空指针异常错误。
public void shorten()
{
// put your code here
if( samples.length % 2 == 0){
double [] temp = new double[samples.length / 2];
}
else if( samples.length % 2 != 0){
double [] temp = new double[samples.length / 2 - 1];
}
Arrays.fill(temp, 1.0);
int j = 0;
for(int i=0; i<= temp.length; i++){
temp[i] = samples[j];
j = j + 2;
}
samples = temp;
}