我正在编写一个应用程序的代码,当我只输入 2 个数字时,我得到 indexoutofbounds Length=2 Index=2。为什么会这样/我该如何规避它?这是我的代码:
Arrays.sort(newNums);
double median = 0;
if(newNums.length%2!=0){
median = (double) newNums[newNums.length/2];
}else if(newNums.length%2==0){
median = ( (double) newNums[newNums.length/2 + 1] + (double) newNums[newNums.length/2-1] )/2;
}else{
}
String medianString = Double.toString(median);
showDialog(medianString, type,"Median:");