I am not good at handling exceptions, so I need a hint at this occasion: I want to put arrays in a collection (ArrayList) all of which should be of the same length. Otherwise errors emerge in computations. When an array of not desired length is to be inserted in the ArrayList, I would like to throw an exception with a message. What kind of exception is suitable for this occasion?
我担心的是,我必须检查将要加插的阵列的规模(如果说的话)。 是否有理由在审判中作陈述?
这里是相关的碎片代码:
投入数据:阵列
阵列:第一阵列规定的阵列长度
有些人能否修改尝试——追赶障碍?
public void insertData(double[] arraydata){
if(this.inputdata.isEmpty()){
inputdata.add(arraydata);
this.arraylength = arraydata.length;
}else{
try {
if(this.arraylength == arraydata.length)
inputdata.add(arraydata);
}catch(Exception exception){
System.err.printf("Missmatch array dimensions in %d place",inputdata.size()+1);
}
}
}