我想提出這個聲音處理程序,但我不斷收到此錯誤:爲什麼我的Java深層複製代碼告訴我「作業需要深度而非淺層的副本」?
Assignment requires a deep, not shallow copy. Are you just copying pointers, or copying the contents of the array?...
我想我對我的公共無效集(double[] mySamples
)做一個淺拷貝,但我是新來的Java和真的不知道該怎麼做。
public class Sound
{
private double[] temp;
private double[] samples;
public Sound() {
samples = null;
}
public Sound(Sound s) {
int j = 0;
double[] source = s.samples;
double temp[] = new double[source.length];
for(int i = 0; i < source.length; i++) {
temp[j] = source[i];
}
samples = temp;
}
public double[] get() {
return samples;
}
public void set(double[] mySamples) {
if (mySamples == null) {
throw new IllegalArgumentException("samples cannot be null");
} else {
samples = mySamples;
}
}
public void increaseVol(double percent) {
double [] result = new double[samples.length];
for (int i = 0; i < samples.length; i++) {
double reduce = samples[i] * percent;
result[i] = samples[i] + reduce;
}
samples = result;
}
public void wavSave(java.lang.String fileName) {
WavIO.write(fileName, samples);
}
}
請編輯您的代碼,以便顯示合理的縮進。請指出哪一行顯示錯誤。 –
這是我第一次在這個網站上,真的不知道你明智的縮進是什麼意思。無論如何,感謝您的幫助。 – user3079090
您可以將空格添加到應縮進的代碼行中,如代碼塊中的代碼。這與本網站無關,全部是關於基本的Java代碼格式。這很重要,因爲閱讀代碼越容易,幫助你就越容易。現在再次,哪一行顯示你的錯誤? –