如何使Collection或Array保存double类型变量的值

时间:2015-01-28 05:49:44

标签: java double

修改: -

如何启用任何Collection或Array来保存rowValue中的值,该值为double类型,以便可以在浏览器上显示值。

public  double[] FindClosestToMultiplesOfTen() {
double rowValue;       
 double []arr=new double[22];
     int i=0;
     try
        {  
       con = getConnection();
          stmt = con.createStatement();
          String sql="some query";
       stmt.executeQuery(sql);
       rs = stmt.getResultSet();

       while(rs.next()) 
        {
           for(int j=0; j<1; j++)
             {
               vals[i]  = rs.getDouble(2);
             }
            i++;
         }
        }
     catch( Exception e )
        {
            System.out.println("\nException "+e);
        }

    double max = Double.MIN_VALUE;
    for (double v : vals) max = Math.max(max, v);
    int bucketCount = 1 + (int)(max/10);
    double[][] buckets = new double[bucketCount][3];
    for (int i1 = 0; i1 < bucketCount; i1++){

        buckets[i1][0] = Double.MAX_VALUE;
    }

    //  iterate the rows
    for (int i1 = 0; i1 < vals.length; i1++)
    {
        double v = vals[i1];
       double mult = getMultipleOfTen(v);
       double delta = Math.abs(mult - v);
       int bIdx = (int)(mult / 10d);
       if (buckets[bIdx][0] > delta)
        {
          buckets[bIdx][0] = delta;
            buckets[bIdx][1] = i1;
            buckets[bIdx][2] = v;
        }
    }

    for (int i1 =1; i1 < buckets.length; i1++)
    {
        double[] bucket = buckets[i1];
        rowValue = bucket[2];
        System.out.println(rowValue);
    }
   return rowValue ;
}

我想将rowValue转换为数组,以便我可以在浏览器中显示其值。目前只显示rowValue的最后一个值,因为我已将其作为double类型变量。 我试过这种方式

 for (int i1 =1; i1 < buckets.length; i1++)
    {
        bucket = buckets[i1];

        System.out.println(bucket);

    }
return bucket;

但是当我运行此代码时,尽管存在桶值,我得到@ form的值。如何解决它?

  

[d @ 1108727

     

[d @ 191e4c

     

[d @ 11415c8

     

[D @ 1a220e6 -----

1 个答案:

答案 0 :(得分:0)

Java数组不会覆盖toString(),因此您从toString()获取默认Object。您可以使用Arrays.toString(double[])之类的

// System.out.println(bucket);
System.out.println(Arrays.toString(bucket));

您也可以使用Arrays.deepToString(Object[])

一次打印所有存储桶
System.out.println(Arrays.deepToString(buckets));