繁体   English   中英

具有通用arraylist的通用类

[英]Generic class with generic arraylist

希望我能对此有所帮助。 我试图实例化具有通用arrayList作为成绩列表的通用对象Student。 我无法使我的Add方法正常工作。 我一直收到一个空指针异常-这很明显,因为它被初始化为null-只是不确定我的add方法出了什么问题。 任何帮助将不胜感激。 另外,此作业的要求非常严格-我必须这样做。 谢谢。

我有两节课:第一节,学生:

import java.util.ArrayList;

public class Student <S>{

    private String name;
    private ArrayList<S> grades;



    private double average;


    // the constructor
    public Student (String studentName, ArrayList<S> studentGrades){
        name = studentName;
        grades = studentGrades;
    }

    // get the name
    public String getName() {
        return name;
    }

    //set the name
    public void setName(String name) {
        this.name = name;
    }

    // add a grade to the array
    public void addGrade(S n){

        grades.add(n);

    }

    // return a grade from the array. This will be used for the calculation.
    public S getGrade(int n){

        return grades.get(n);


    }

    // compute the average grade for each student
    public double computeAverage(){

        Double sum = 0.00;
        for(S grade : grades){ 

            if (grade instanceof Double){
            sum += (Double)grade;
            }
            if (grade instanceof Integer){
                sum += (Integer)grade;
            }
        }
        average = sum.doubleValue()/ grades.size();
        return average;

    }

    public String toString()
    {
        return name + "'s average grade is " + average;
    }
}

第二,测试:

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        // create the studentList array
        ArrayList<Student> studentList = new ArrayList<Student>();
        HashMap<String, Student> studentMap = new HashMap<String, Student>();

        // the values for math.random to create the grades
        int min = 0;
        int max = 100;
        double dmin = 0.00;
        double dmax = 100.00;

        // initialize the variables

        ArrayList grades = null;

        //------------------------------------
        // Create the students (the method signature is wrong. It blows up regardless of what I try to add.)
        Student<Integer> Fred = new Student<Integer>("Fred", grades);   
        //null pointer exception. Also points to the add method in the student class.

        //-------------------------------------

        Student<Integer> Wilma = new Student<Integer>("Wilma", grades);
        Student<Double> Barney = new Student<Double>("Barney", grades);
        Student<Double> Betty = new Student<Double>("Betty", grades);   

        // add the random grades 
        for(int i = 0; i < 5; i++){
            Fred.addGrade((int) (Math.random() * (max - min) + min));
        }

        for(int i = 0; i < 5; i++){
            Wilma.addGrade((int) (Math.random() * (max - min) + min));
        }

        for(int i = 0; i < 5; i++){
            Barney.addGrade((double) (Math.random() * (dmax - dmin) + dmin));
        }

        for(int i = 0; i < 5; i++){
            Betty.addGrade((double) (Math.random() * (dmax - dmin) + dmin));
        }
        // add the students to the array list
        studentList.add(Fred);
        studentList.add(Wilma);
        studentList.add(Barney);
        studentList.add(Betty);

        studentMap.put(Fred.getName(), Fred);
        studentMap.put(Wilma.getName(), Wilma);
        studentMap.put(Barney.getName(), Barney);
        studentMap.put(Betty.getName(), Betty);


        //iterate through the list & print to the console
        Iterator<Student> itr = studentList.iterator();{
            while(itr.hasNext()){
                System.out.println(itr.next().<Student>toString());

                //Initialize the array to hold the key variables
                Set<String> mapKeys = studentMap.keySet();
                //mapKeys.add(something);

            }

        }

    }
}

您需要初始化ArrayList等级。 当前,您正在传递一个空值。 在该行中执行以下操作:

ArrayList grades = new ArrayList();

一个好的做法是在初始化ArrayList时使用泛型,但是由于您的成绩似乎是不同的类型,因此我将其跳过。

在您的Test类中,您具有ArrayList grades = null; -您需要先创建成绩ArrayList并使用数据填充它,然后再使用它。

您正在初始化测试成绩为空

ArrayList grades = null;

当您尝试添加成绩时

Fred.addGrade(...);

学生的内部代码

grades.add(n);

但是将等级初始化为null,从而创建Null Exception。

用适当的类型初始化成绩。

例:

Student<Integer> Wilma = new Student<Integer>("Wilma", new ArrayList<Integer>());
Student<Double> Barney = new Student<Double>("Barney", new ArrayList<Double>());
Student<Double> Betty = new Student<Double>("Betty", new ArrayList<Double>());   

您可以执行以下操作:

public class Student<S>
{
    // declare array
    private final ArrayList<S> grades = new ArrayList<S>();
    private String name;

    // One arg constructor
    public Student(String name)
    {
        this.name = name;
    }

    // Two arg constructor
    public Student(String name, ArrayList<S> grades)
    {
        this(name);
        this.grades.addAll(grades);
    }

    // etc
}

在测试类中以以下方式编辑代码:

 Student<Integer> Fred = new Student<Integer>("Fred", new ArrayList<Integer>());   
    //null pointer exception. Also points to the add method in the student class.

    //-------------------------------------

 Student<Integer> Wilma = new Student<Integer>("Wilma", new ArrayList<Integer>());
 Student<Double> Barney = new Student<Double>("Barney", new ArrayList<Double>());
 Student<Double> Betty = new Student<Double>("Betty", new ArrayList<Double>());   

希望这会有所帮助。

暂无
暂无

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

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