Java 将浮动从arrayList放置到等宽度的容器中以创建直方图
此代码当前将接收一个数组列表,并从包含的浮动中创建10个等宽的容器;然后它将打印箱子范围。我遇到的问题是如何通过ArrayList建立索引,并根据找到的浮点在正确的容器中打印星号 我不是要密码;而是朝着正确的方向推进Java 将浮动从arrayList放置到等宽度的容器中以创建直方图,java,Java,此代码当前将接收一个数组列表,并从包含的浮动中创建10个等宽的容器;然后它将打印箱子范围。我遇到的问题是如何通过ArrayList建立索引,并根据找到的浮点在正确的容器中打印星号 我不是要密码;而是朝着正确的方向推进 public static String getHist( String Tag, ArrayList <Float> x) { Float max = getMaximum(x); Float min = getMinimum(x); F
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;
}
publicstaticstringgethist(stringtag,arraylistx)
{
Float max=getmax(x);
浮动最小值=最小值(x);
浮动间隔=(最大-最小)/10f;
//系统输出打印(“间隔:+间隔);
浮动底座=最小值;
//系统输出打印项次(标签);
//循环10次迭代
对于(浮点数i=base;ifor)(浮点数n:x)
{
如果(i.与(n)0相比)
系统输出打印(“*”);
else如果(i==max&&n==max)
{
系统输出打印(“*”);
}
}
在打印*
之前,您需要检查n
的值是否在垃圾箱的范围内。您当前正在为x
中的每个值打印一个*
,无论它是否在您当前的范围内。是的,我刚刚做了这项工作,以确保*打印在正确的位置。您可以创建一个基数组并检查x是否大于或等于base[c]
和小于base[c+1]
所以每个箱子的最大和最小值都有一个基数?您只需要基数或最大值;您需要a>=base[c]&a
,因为base[c+1]
是上一个基数的上限。如果跟踪max,则将其更改为a base[c-1]
。在搜索正确的bin时,每个bin都有自己的约束(无论是对数组使用二进制搜索,还是对其进行迭代或其他启发式搜索)
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("*");
}
}