具有通用arraylist的泛型类

时间:2014-02-24 16:56:53

标签: java generics 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);

            }

        }

    }
}

5 个答案:

答案 0 :(得分:2)

您需要初始化ArrayList成绩。目前您传递的是空值。在该行中执行以下操作:

ArrayList grades = new ArrayList();

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

答案 1 :(得分:1)

在你的Test课程中 ArrayList grades = null; - 您需要创建成绩ArrayList并在使用前填充数据。

答案 2 :(得分:1)

您正在初始化将等级测试为空

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>());   

答案 3 :(得分:0)

你可以这样做:

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
}

答案 4 :(得分:0)

在Test class中按以下方式编辑代码:

 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>());   

希望,这会有所帮助。