如何从Java中的Class返回ArrayList值

时间:2013-10-23 05:16:45

标签: arraylist

这是我的代码:代码应返回带有arraylist相关数据的学生列表。但错误告诉我它不能对非静态方法进行静态引用?

我试图让这些方法保持静态,它给了我另一个错误。

//main function code
    String forename = null;
    String surname = null;
    String grade = null;
    String yesOrNo;
    double mark;
    int selection;

ArrayList<StudentClass> studentDetails = new ArrayList<StudentClass>();

switch(selection){
case 1: {
    if (studentDetails.isEmpty()){
        System.out.println("No Students Have Been Entered Yet");
        main(null);
        break;
                }
    else{
         for(int i = 0; i < studentDetails.size(); i++){
             StudentClass = studentDetails.get(i);
             System.out.println( StudentClass.getForename() + " " +
             StudentClass.getSurname() + " received a " + StudentClass.getGrade() +
             " for their Student Mark of " + StudentClass.getMark() + "." );
            }
        }
break;
\\ Error:Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
StudentClass cannot be resolved to a variable
Cannot make a static reference to the non-static method getForename() from the   type StudentClass
Cannot make a static reference to the non-static method getSurname() from the type StudentClass
Cannot make a static reference to the non-static method getGrade() from the type StudentClass
Cannot make a static reference to the non-static method getMark() from the type StudentClass

at students.main(students.java:60)
\\code for Class
public class  StudentClass {
      public String Forename;
      public String Surname;
      public  String Grade;
       public  double Mark;

public StudentClass(String forename, String surname, double mark){

       Forename = forename;
   Surname = surname;
   Mark = mark;
   }

  public void setForename(String forename)
    {
        Forename= forename;
    }

  public void setSurname(String surname)
    {
        Surname= surname;
    }

  public void setMark(double mark)
    {
        Mark= mark;
    }

  public  String getForename()
    {
        return Forename;
    }

  public  String getSurname()
    {
        return Surname;
    }

  public  double getMark()
    {
        return Mark;
    }
  public  String getGrade()
    {   
            if ( Mark < 40 )
                Grade = "FAIL";
            else if ( (Mark >= 40) && (Mark <= 64) )
                Grade ="PASS";
            else if ( (Mark >= 65) && (Mark <= 84) )
                Grade ="MERIT";
            else if ( (Mark >= 85) && (Mark <= 100) )
                Grade ="DISTINCTION";
            return Grade;
}
  }

4 个答案:

答案 0 :(得分:1)

StudentClass = studentDetails.get(i);毫无意义。 StudentClass是一个类名。

您需要一个实例:StudentClass student = studentDetails.get(i);然后使用student.getSurname()等。

答案 1 :(得分:0)

StudentClass.getSurname()未定义为静态方法。您需要声明该类的实例才能调用它。

StudentClass myClass = new StudentClass(/* whatever params you need*/);
String surname = myClass.getSurname();

请记住,您需要为所有这些方法执行此操作,因为它们位于实例级别。

答案 2 :(得分:0)

for(int i = 0; i < studentDetails.size(); i++){
             StudentClass = studentDetails.get(i);
             System.out.println( StudentClass.getForename() + " " +
             StudentClass.getSurname() + " received a " + StudentClass.getGrade() +
             " for their Student Mark of " + StudentClass.getMark() + "." );
            }

将上面的代码更改为:

    for(int i = 0; i < studentDetails.size(); i++){
        StudentClass student = studentDetails.get(i);
        System.out.println( student.getForename() + " " +
        student.getSurname() + " received a " + student.getGrade() +
                 " for their Student Mark of " + student.getMark() + "." );
   }

查看错误是否消失。编译器抱怨,因为您试图在类本身(而不是类的实例)上调用实例级(非静态)方法。

答案 3 :(得分:0)

使用对象

无法访问java中的非静态方法

试试这个,

else{
         for(int i = 0; i < studentDetails.size(); i++){
             studentDetails.get(i) = new StudentClass();
             System.out.println( studentDetails.get(i).getForename() + " " +
             studentDetails.get(i).getSurname() + " received a " + studentDetails.get(i).getGrade() +
             " for their Student Mark of " + studentDetails.get(i).getMark() + "." );
            }
        }