如何从java中的数组中删除索引1和0

时间:2015-03-24 06:27:55

标签: java arrays

我有一个方法,我有一个数组来保存sql查询中的值,并对数组进行处理以检索所需的结果。现在我要删除值或从数组中删除索引0和1处的元素。如何做到这一点。由于我得到的结果集没有当前行的异常,只要数组的索引达到0或1.我想在索引0和1处删除桶数组中的元素在以下代码的最后一个for循环中 -

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

        int row_id ;
        int bIdx = 0;

        //double[] vals = new double[47];
        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.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') ";

                  System.out.println("Value of sql of FindClosestToMultiplesOfTen is"+sql);
                  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);
                   System.out.println("value of beam_current at closest is "+vals[i]);
                 }
                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);
        int bucketCount =1+(int)(max/10);
        double[] bucket =new double[bucketCount];

        System.out.println("bucketcount in closese"+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)
            {

              buckets[bIdx][0] = delta;
              buckets[bIdx][1] = row_id;
              buckets[bIdx][2] = v;

            }
         }  

        for (int i1 =( buckets.length)-1;i1>0; 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);

    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;
    }

2 个答案:

答案 0 :(得分:0)

 for (int i1 =( buckets.length)-1;i1>0; i1--) 

如果您对索引为0&amp;的元素有疑问1,你不想要它们,而不是试图删除它们

你可以通过for循环中的小修改来避免访问它们。

for(int i1 =(buckets.length)-1; i1&gt; = 2; i1--)

否则,如果你真的想删除,那么你必须做类似以下的事情。

     for (int i1 =0;i1<( buckets.length-2); i1++) 
      buckets[i] = buckets[i+2]; 
      buckets[buckets.length-1] = null;
      buckets[buckets.length-2] = null;

答案 1 :(得分:0)

您无法对数组进行更改,但您可以声明一个新数组,也可以使用这样的arraylist:

arrayList<String> arrayListNamet = new ArrayList<String>();
arrayListName.remove(0);
arrayListName.remove("element");