【问题标题】:null and outofbounds exceptionsnull 和 outofbounds 异常
【发布时间】:2014-05-20 13:55:07
【问题描述】:

我有一个读取样本数组的声音类。它使用 WavIO 类来执行此操作。如果WavIO.read() 返回null,我希望它什么也不做。我该怎么办?不确定它是 set 还是 get 方法或两者兼而有之。另外,我遇​​到了这两个错误:

symptom: java.lang.ArrayIndexOutOfBoundsException: 7
at Sound.shorten(Sound.java:69)

symptom: java.lang.ArrayIndexOutOfBoundsException: 3
at Sound.lengthen(Sound.java:51)


public class Sound {   
private String fileName;
private double [] samples; 
public Sound(){
    this.samples = new double[0];
}

public Sound(Sound pSound){
    double[] temp;
    temp = new double[pSound.samples.length];
    for(int i = 0; i < temp.length; i++){
        temp[i] = pSound.samples[i];
    }
    this.samples = temp;
}

public double[] get(){
    return samples;
}

public void increaseVol(double percent){
    // percent = percent/100.0;
    for(int i = 0; i<samples.length;i++){
        samples[i] = samples[i] * (1.0 + percent);
    }
}

public void reduceVol(double percent){
    //percent = percent/100.0;
    for(int i = 0; i<samples.length;i++){
        samples[i] = samples[i] * (1.0 - percent);
    }
}

public void lengthen(){ 
    double[]t = new double[samples.length];

    for(int i = 0;i<samples.length;i++){
        t[i] = samples[i];
    }
    samples = new double[t.length*2];
    for(int i = 0; i < samples.length;i++){
        samples[(2*i)] = t[i];
        samples[(2*i)+1] = t[i];
    } 
}

public void shorten(){
    double[]t = new double[samples.length];
    for(int i = 0; i < samples.length;i++){
        t[i] = samples[i];
    }
    samples = new double[t.length/2];
    if(samples.length % 2 == 0){
        for(int i = 0; i <samples.length;i++){
            samples[i] = t[2*i];
        }
    }
    else{
        for(int i = 0; i <samples.length+1;i++){
            samples[i] = t[(2*i)+1];
        }
    }
}

public void reverse(){
    for(int i = 0; i<samples.length; i++){
        samples[i] = samples[(samples.length)-1-i];
    }
}

public void set(double[] mySamples){
    if (mySamples == null) {
        throw new IllegalArgumentException("null");
    } else {
        samples = new double[mySamples.length];
        for (int i = 0; i < mySamples.length; i++) {
            samples[i] = mySamples[i];
        }
    }
}    

public void wavRead(String fileName){
    this.samples = WavIO.read(fileName);

}

public void wavSave(String fileName){
    WavIO.write(fileName,samples);
}
}

【问题讨论】:

  • 请用 cmets 标记这两行

标签: java arrays exception methods wav


【解决方案1】:

对于您提到的第二个 ArrayOutOfBounds 异常,

在您的 lengthen 方法中,您有这个循环:

samples = new double[t.length*2];
for(int i = 0; i < samples.length;i++){
    samples[(2*i)] = t[i];
    samples[(2*i)+1] = t[i];
} 

循环应该是:

samples = new double[t.length*2];
for(int i = 0; i < samples.length;i++){
    samples[i] = t[i/2];
} 

您已经将samples 的长度增加了一倍,但您的循环长度增加了samples。将其加倍将使i 在循环结束附近的样本长度几乎增加一倍。

对于您的其他 ArrayIndexOutOfBounds 异常,在您的 shorten 方法中,您有:

else{
    for(int i = 0; i <samples.length+1;i++){  // remove +1
        samples[i] = t[(2*i)+1];              // remove +1
    }
}

在这种情况下,您已将 samples 数组的大小减半,并使用 t 数组中的元素填充它,只占用第二个元素。您可以删除 +1,因为它们会导致代码尝试执行超出数组长度的操作。

else{
    for(int i = 0; i <samples.length;i++){    
        samples[i] = t[2*i];                   
    }
}

其实你的整个shorten方法可以缩短:

 public void shorten(){
    double[]t = new double[samples.length];

    for(int i = 0; i < samples.length;i++){
        t[i] = samples[i];
    }
    samples = new double[t.length/2];

    for(int i = 0; i <samples.length;i++){
        samples[i] = t[2*i];
    }
}

【讨论】:

  • 那么数组应该是长度的两倍。所以如果它的 ABC 它应该是 AABBCC
  • 我不是在争论数组应该是双倍的长度。我是说在循环中,当您将元素放入samples 时,不应将 i 的值加倍
  • 我不确定你的意思。我仍然是一个初学者程序员,所以我不确定我的代码有什么问题。
  • @user3284325 没关系。我不介意帮助初学者。你在正确的轨道上。我相信您已经发现,您遇到的例外是因为您的 samples 数组中只有这么多点,但代码将数组视为有更多点。当数组对于您的代码尝试执行的操作而言太小时,您会得到 ArrayIndexOutOfBoundsException
  • 如果数组只有 10 个点长,程序可能会尝试将一些东西放入数组的第 11 个点。这将导致ArrayIndexOutOfBoundsException
猜你喜欢
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
相关资源
最近更新 更多