将来自arrayList的浮点放置到相等的宽度区域中以创建直方图

时间:2014-01-31 00:11:32

标签: java

此代码目前将包含一个数组列表,并从包含的浮点数创建10个相等宽度的bin;然后它将打印bin范围。我遇到的问题是弄清楚如何索引ArrayList并根据找到的float在正确的bin中打印一个星号。

我不是要求代码;而是朝着正确的方向发展。

 public static String getHist( String Tag, ArrayList <Float> x)
{

    Float max = getMaximum(x);
    Float min = getMinimum(x);

    Float interval = (max - min) / 10f;
   // System.out.print("Interval:" + interval);
    Float base = min;
    //System.out.println(Tag);

    //Loops through for 10 iterations
    for(Float i = base ; i <= (max - interval); i+= interval)
    {
        //System.out.print("Interval:" + i + "\n");
        //prints out the bin range
        System.out.print(i + " - " + (base += interval) + " | ");

    //Loop through ArrayList and if a number found belongs within a bin place it
    // within that appropiate bin

    //Logic not correct?
        for(Float index = i; index <= base; index++)
        {
               for(Float n : x)
               {

            System.out.print("*");
               }
        }
        System.out.println();
        }
    return Tag;
}

1 个答案:

答案 0 :(得分:0)

for(Float n : x)
        {
            if (i.compareTo(n) <= 0 &&  base.compareTo(n) > 0)
            System.out.print("*");

            else if (i == max && n == max)
            {
                System.out.print("*");

            }
        }