Java中等级数组列表的直方图
我试图从一个包含学生成绩的数组列表中创建一个直方图。我已经做了一个等级分类,如下所示:Java中等级数组列表的直方图,java,arrays,arraylist,histogram,Java,Arrays,Arraylist,Histogram,我试图从一个包含学生成绩的数组列表中创建一个直方图。我已经做了一个等级分类,如下所示: /** * Returns a simple, 5-element array with the counts for each of the letter grades, * (A, B, C, D, and F), based on the 10-point scale * @return 5-element array */ private int[] calculateGradingBreak
/**
* Returns a simple, 5-element array with the counts for each of the letter grades,
* (A, B, C, D, and F), based on the 10-point scale
* @return 5-element array
*/
private int[] calculateGradingBreakdown() {
int[] breakdown;
breakdown = new int[7];
for (Student kids: this.students) {
int grade = kids.getNumericGrade();
if (grade >= 90) {
breakdown[0] += 1;
} else if (grade >= 80) {
breakdown[1] += 1;
} else if (grade >= 70) {
breakdown[2] += 1;
} else if (grade >= 60) {
breakdown[3] += 1;
} else {
breakdown[4] += 1;
}
}
return breakdown;
}
/**
* Returns a string that lists the grade letter and the count of students
* receiving the grade on the roster
* @return grade breakdown
*/
public String getGradeBreakdown() {
String gradeBreakdown = null;
int[] breakdown = this.calculateGradingBreakdown();
gradeBreakdown = ("A: " + breakdown[0] + "\nB: " + breakdown[1] + "\nC: " + breakdown[2]
+ "\nD: " + breakdown[3] + "\nF: " + breakdown[4]);
return gradeBreakdown;
}
我为直方图编写的代码已经更改了几次,但需要包括下面列出的方法。我已经留下了我当前的代码,但是对于如何让柱状图如清单所示工作,我感到很困难
/**
* Accepts a number of stars (*) to be created, creates a String with that
* number of *'s side-by-side, and then returns that string.
*/
private String makeStarRow(int number) {
int[] breakdown = this.calculateGradingBreakdown();
number = breakdown[];
String stars =
}
/**
* Returns a string holding a horizontal histogram of *'s
*/
public String getGradeHistogram() {
String gradeHistogram = null;
int[] breakdown = this.calculateGradingBreakdown();
gradeHistogram = (this.makeStarRow(breakdown[0]));
gradeHistogram += (this.makeStarRow(breakdown[1]));
gradeHistogram += (this.makeStarRow(breakdown[2]));
gradeHistogram += (this.makeStarRow(breakdown[3]));
gradeHistogram += (this.makeStarRow(breakdown[4]));
return gradeHistogram;
}
输出应如下所示,以成绩细分和直方图结束(数字根据另一个类中的输入):
创建重复符号字符串的方法之一是使用:
请注意,根据
getGradeHistogram
方法,可能需要将'\n'
附加到stars字符串的末尾。谢谢大家的帮助。我实际上得到了一个有效的解决方案:
/**
* Accepts a number of stars (*) to be created, creates a String with that
* number of *'s side-by-side, and then returns that string.
*/
private String makeStarRow(int number) {
while (number > 0) {
System.out.print("*");
number--;
}
if (number < 1) {
System.out.println();
}
return null;
}
/**
* Returns a string holding a horizontal histogram of *'s
* @return histogram
*/
public String getGradeHistogram() {
int[] histogram = this.calculateGradingBreakdown();
for (int xi = 0; xi < this.students.size(); xi++) {
int meh = histogram[xi];
this.makeStarRow(meh);
}
return "";
}
/**
*接受要创建的多个星号(*),并使用该星号创建一个字符串
*并排的*数,然后返回该字符串。
*/
私有字符串makeStarRow(整数){
而(数量>0){
系统输出打印(“*”);
数字--;
}
如果(数字<1){
System.out.println();
}
返回null;
}
/**
*返回包含*水平直方图的字符串
*@返回直方图
*/
公共字符串getGradeHistogram(){
int[]直方图=this.calculateGradingBreakdown();
对于(int x= 0;席< this .Susior)(席)(席)
int-meh=直方图[xi];
这是马克斯塔罗(meh);
}
返回“”;
}
它打印出我要找的东西。希望这对将来的人有所帮助
A:2
B:2
C:2
D:0
F:1
**
**
**
*为了您的兴趣和参考,这里有一个使用Java 8流的解决方案:
void printHistogram(List<Integer> scores) {
scores.stream().collect(Collectors.groupingBy(n -> n < 60 ? 5 : n / 10));
.entrySet().stream().sorted(Map.Entry::comparingByKey)
.map(entry -> entry.getValue().size());
.map(size -> Stream.iterate(() -> "*").limit(size).collect(Collectors.joining()))
.forEach(System.out::println);
}
void打印直方图(列出分数){
scores.stream().collect(收集器.groupingBy(n->n<60?5:n/10));
.entrySet().stream().sorted(Map.Entry::comparingByKey)
.map(entry->entry.getValue().size());
.map(size->Stream.iterate(()->“*”).limit(size).collect(Collectors.joining()))
.forEach(System.out::println);
}
那么问题是什么?如何实现makeStarRow()
?你的javadoc说明了一切。照上面说的做。为什么要从char[]
构建一个字符串,然后进行字符串连接以添加一个字符?字符串连接意味着new StringBuilder().append(新字符串(charChars)).append('\n').toString()
。哎呀!!将数组放大一个,并将最后一个字符设置为\n
。或者更大的两个,并将最后两个设置为\r
和\n
@Andreas,这样的优化对OP程序的整体性能没有任何意义,但使其更难维护(用其他东西替换“\n”也需要修补数组长度)。还可以通过JIT附加单个字符。单数组复制仍将发生,但不会实际创建带有中间数组的StringBuilder实例。@Andreas,我的基准测试表明,对于热代码,每行1-10星可以节省20纳秒,每行100星可以节省200纳秒。这种过早的优化不会给将这些行打印到标准输出的应用程序添加任何内容。打印(甚至重定向到nul)至少需要100倍以上的时间。
/**
* Accepts a number of stars (*) to be created, creates a String with that
* number of *'s side-by-side, and then returns that string.
*/
private String makeStarRow(int number) {
while (number > 0) {
System.out.print("*");
number--;
}
if (number < 1) {
System.out.println();
}
return null;
}
/**
* Returns a string holding a horizontal histogram of *'s
* @return histogram
*/
public String getGradeHistogram() {
int[] histogram = this.calculateGradingBreakdown();
for (int xi = 0; xi < this.students.size(); xi++) {
int meh = histogram[xi];
this.makeStarRow(meh);
}
return "";
}
void printHistogram(List<Integer> scores) {
scores.stream().collect(Collectors.groupingBy(n -> n < 60 ? 5 : n / 10));
.entrySet().stream().sorted(Map.Entry::comparingByKey)
.map(entry -> entry.getValue().size());
.map(size -> Stream.iterate(() -> "*").limit(size).collect(Collectors.joining()))
.forEach(System.out::println);
}