java - java中ArrayIndex越界异常

标签 java arrays indexoutofboundsexception

我有一个jsp页面,通过它我可以从具有数据时间格式的下拉列表中选择一个值,并且我有一个日历,可以从中选择数据和时间。当我提交此表单时,会调用另一个JSP页面,该页面调用一个方法在java类中定义。 该方法有如下代码

public  LinkedHashMap<Double, String> ClosestToMultiplesOfTen_User(String start,String end) throws SQLException {

int row_id ;
int bIdx = 0;
double[] vals=null;
int rowIndex = 0 ;
int i=0;

try
        { 
          con = getConnection();
          stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);     
   String sql="select distinct beam_current from INDUS2_BDS.dbo.DCCT where logtime between '"+start+"' and '"+end+"'"+
                  "and (beam_current like '%9.95' or beam_current like '%9.96' or beam_current like '%9.97' or beam_current like '%9.98' or  beam_current like '%9.99'  or beam_current like '%0' or beam_current like '%_0.01' or beam_current like '%_0.02' or beam_current like '%_0.03' or beam_current like '%_0.04' or beam_current like '%_0.05' or beam_current like '%_0.06') ";

         stmt.executeQuery(sql);
          rs = stmt.getResultSet();

         rs.last();
            int row_cnt=rs.getRow();
            System.out.println("row_count of closest " +row_cnt);
             vals = new double[row_cnt];
            rs.beforeFirst();
while(rs.next()) 
    {
   for(int j=0; j<1; j++)
         {
           vals[i]  = rs.getDouble(1);
         }
        i++;
     }
    }
 catch( Exception e )
    {
        System.out.println("\nException "+e);
    }


//  get the max value, and its multiple of ten to get the number of buckets
 double max = java.lang.Double.MIN_VALUE;
 for (double v : vals) max = Math.max(max, v);
Arrays.sort(vals);
System.out.println("value at vals[0] c "+vals[0]);
double min=vals[0];
int m2=(int) Math.round(min);
int m3=(int) Math.round(max);

int bucketCount = 1+((m3-m2)/10);
double[] bucket =new double[bucketCount];

 //  initialise the buckets array to store the closest values
double[][] buckets = new double[bucketCount][3];
for (int i1 = 0; i1 < bucketCount; i1++){
     // store the current smallest delta in the first element
     buckets[i1][0] = java.lang.Double.MAX_VALUE;
     // store the current "closest" index in the second element
     buckets[i1][1] = -1d;
     // store the current "closest" value in the third element
     buckets[i1][2] = java.lang.Double.MAX_VALUE;
 }

 //  iterate the rows
 for (row_id=1 ; row_id < vals.length; row_id++)
 {
     //  get the value from the row
     double v = vals[row_id];
     //  get the closest multiple of ten to v
     double mult = getMultipleOfTen(v);
     //  get the absolute distance of v from the multiple of ten
     double delta = Math.abs(mult - v);
     //  get the bucket index based on the value of `mult`
    bIdx = (int)(mult / 10d);
   // System.out.println("value of bidx for bucket index is"+bIdx);
     //    test the last known "smallest delta" for this bucket
     if (buckets[bIdx][0] > delta)
     {
      //  this is closer than the last known "smallest delta"
       buckets[bIdx][0] = delta;
       buckets[bIdx][1] = row_id;
       buckets[bIdx][2] = v;

     }
  }  
//   print out the result
for (int i1 =1; i1 < buckets.length; i1++)
{
      bucket = buckets[i1];
     rowIndex = (int) bucket[1];
 double rowValue = bucket[2];
      DecimalFormat twoDForm = new DecimalFormat("#.##"); 
      System.out.println("row index closeset "+rowIndex+ "value is closest "+rowValue);
   rs.absolute(rowIndex+1);
      user_current_map.put(java.lang.Double.valueOf(twoDForm.format(rs.getDouble(1))),"");

     }
System.out.println("user_current_map "+user_current_map);

return user_current_map;
}

 public  double getMultipleOfTen(double v)
            {
                 System.out.println(10d * Math.round(v / 10d));
                return 10d * Math.round(v / 10d);
            }

检索到的存储桶计数值正确。但我在 java.lang.ArrayIndexOutOfBoundsException:21if (buckets[bIdx][0] > delta) 行遇到异常。 21 是从桶计数中检索到的值。我没有得到我的错误,我错在哪里??

最佳答案

我明白了,**bIdx = (int)(mult/10d);** 从 m2/10 值开始,而不是从 0 开始 ,因此当它尝试访问大于等于桶计数的桶大小的索引时,就会发生数组索引越界异常。

解= bIdx = (int)(mult/10d) - m2/10;

除以检索到的最小值/10 ,这样它就会从 0 开始,一直到桶大小。

关于java - java中ArrayIndex越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272118/

相关文章:

java - 需要帮助-在sql Developer3.0中查询将图像插入表中

java - 使用 Firebase 注册和登录 Android 应用程序的最佳方式是什么?

python - 从两个矩阵乘积的条目填充 4d 数组的有效方法

Javascript如何查找数组中对象的位置

android - 添加项目后,Recyclerview 将索引绑定(bind)到列表大小之外

java - 通过 Java 从 Active Directory 查询帐户名和电子邮件

java - 带有 ETC2 压缩的 PKM 纹理导致黑屏

python - 从 numpy 数组的一列中减去

Java数组排序(我做错了什么)

c# - 更改数组中索引的值,将从旧索引开始的索引向右移动,同时保留左侧的所有索引不变