简体   繁体   English

if语句和for循环中的ArrayList问题

[英]problems with ArrayList in if-statement and for-loop

I have a question regarding the for-loop and ArrayList. 我有关于for循环和ArrayList的问题。 I have a HashMap with String and Integers, where the String represents a class, and the Integer represents the grade. 我有一个带字符串和整数的HashMap,其中String表示一个类,Integer表示成绩。 They I have a collection of students in an ArrayList. 他们在ArrayList中有一组学生。 So what I am trying to do is find the average of the grades of the students in the ArrayList. 所以我要做的是找到ArrayList中学生成绩的平均值。 This is the code of that class: 这是该类的代码:

import java.util.ArrayList;

public class BachelorStudenter {

  private ArrayList<BachelorStudent> bs;

  public Bachelorstudenter() {    
      bs = new ArrayList<>();
  }

  public int bsGjennomsnitt() {    
     int sum = 0;
     int gjennomsnitt = 0;

     if(!bs.isEmpty()){
        for(BachelorStudent b : bs.values){
           sum += b.finnGjennomsnitt();
        }
        return gjennomsnitt;
     }else {        
        return 6;
     }
  } 
}

I know that the bs.values in the for-loop within the if-statement is wrong, but I've tried googling what I should use instead, and neither .contains or .size works. 我知道if语句中的for循环中的bs.values是错误的,但我已经尝试使用谷歌搜索我应该使用的内容,并且.contains或.size都不起作用。

Oh, and I also have another class called BachelorStudent, where I can create an object of the BachelorStudent and it will be put in the ArrayList in Bachelorstudenter. 哦,我还有另一个名为BachelorStudent的课程,在那里我可以创建一个BachelorStudent的对象,它将放在Bachelorstudenter的ArrayList中。

Does anyone know what I need to put there instead? 有人知道我需要放在那里吗?

What you a doing is recursively ending in an exception.... 你做的是递归地以异常结束....

You have to do:: 你必须做::

for(Bachelorstudent b : bs){
     sum += b.getSomething();
}

and define getSomething as a getter for a field in the classs Bachelorstudent 并将getSomething定义为类Bachelorstudent中字段的getter

If what you want to to is to return the average of the average grade over all the students you could do the following: 如果你想要的是返回所有学生的平均成绩的平均值,你可以做以下事情:

import java.util.ArrayList;
public class Bachelorstudenter {

    private ArrayList<Bachelorstudent> bs;

    public Bachelorstudenter() {    
        bs = new ArrayList<>();
    }

    public double bsGjennomsnitt() {    
        double sum = 0.0;
        double gjennomsnitt = 6.0;

        if(!bs.isEmpty()){
            for(Bachelorstudent b : bs){
                sum += b.finnGjennomsnitt();
            }
            gjennomsnitt = sum/bs.size()
        } 
        return gjennomsnitt;
    } 
}

please note that I changed the return value , sum and gjennomsnitt to double as an average may also be a non-integer value. 请注意,我将return valuesumgjennomsnittdouble因为平均值也可能是非整数值。 The answer also assumes that the b.finnGjennomsnitt() returns the average over all classes a student attends and returns this. 答案还假设b.finnGjennomsnitt()返回学生参加的所有课程的平均值并返回此值。 If you only want to return the average for all students in ONE class the answer will not give you this. 如果你只想返回一个班级所有学生的平均分,答案就不会给你这个。 Please specify in your question and also include the code for finnGjennomsnitt() if this is part of the question. 如果这是问题的一部分,请在您的问题中指明并包括finnGjennomsnitt()的代码。 The method in the answer will return 6.0 as average if there are no students, as in the code in the question. 如果没有学生,答案中的方法将返回6.0作为平均值,如问题中的代码。 Perhaps this should be reconsidered as it makes the case of an exceptional group of students with straight 6.0 grades equal to the case of no students. 也许这应该被重新考虑,因为它使一个特殊的学生群体的直接6.0等级的情况等于没有学生的情况。 Perhaps you can return -1 when there are no students? 也许你可以在没有学生时返回-1?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM