用null替换Object数组中的项(java)

时间:2014-05-26 19:14:06

标签: java arrays object null

我开始在课程中学习面向对象,我们有一个名为students的对象类。 students类的对象存储实例变量:studentNamestudentNumberstudentGPA。我在我的对象类中有一个布尔方法来确定学生是否是一个失败的学生(如果他们的GPA> 2.0,学生就失败了)然后我在我的工人类中有一个应该接受数组的方法students个对象,然后如果学生失败,则用" null"替换该对象。

我的问题是我很难用null替换学生对象,因为java一直在抛弃我或者其他一些东西。以下是我尝试过的内容:

public static void removeFailingStudents(Student[] students)
 {
  int count;

  for (count=0; count<students.length; count++)
  {
    if(students[count].isFailing())
    {
      students[count] = null;
    } 
  }
 }

public static void removeFailingStudents(Student[] students)
 {
  int count;

  for (count=0; count<students.length; count++)
  {
    if(students[count].isFailing())
    {
      students[count] = "null";
    } 
  }
 }

但是当我编译/运行这些尝试时,它会抛出异常或者对我大喊大叫,因为它不属于Student类型。如何将对象数组中的项设置为null?谢谢你的帮助!

这是我的完整代码:

public class L2Q1
{
 public static void main(String[] parms)
 {
  process();

  System.out.println("\nEnd of processing.");
 }

 public static void process()
 {
  Student[] students;
  Student[] newStudents;

  students = getStudents();
  printStudents(students);
  printAverageGPA(students);
  printHonourStudents(students);
  removeFailingStudents(students);
  printStudents(students);
  newStudents = compactStudents(students);
  printStudents(students);
  printStudents(newStudents);
 }

 public static void printStudents(Student[] students)
 {
  int count;

  System.out.println("Students:");
  for (count=0; count<students.length; count++)
  {
    System.out.println(students[count].toString());
  }
  System.out.println();
 }

 public static void printAverageGPA(Student[] students)
 {
  double sumGPA;
  int count;

  sumGPA = 0;
  for (count=0; count<students.length; count++)
  {
    sumGPA += students[count].getGPA();  
  }
  double average = sumGPA / count;
  System.out.println("The average GPA is " + average);
  System.out.println();
 }

 public static void printHonourStudents(Student[] students)
 {
  int count;

  System.out.println("Honour students:");
  for (count=0; count<students.length; count++)
  {
    if(students[count].isHonourStudent())
    {
      System.out.println(students[count].toString());
    }
  }
  System.out.println();
 }

 public static void removeFailingStudents(Student[] students)
 {
  int count;

  for (count=0; count<students.length; count++)
  {
    if(students[count].isFailing())
    {
      students[count] = null;
    } 
  }
 }

 public static Student[] compactStudents(Student[] students)
 {
  Student[] newStudents;
  int count1;
  int count2;

  System.out.println("Compacting failing students.");
  System.out.println();
  count1 = 0;
  for (count2=0; count2<students.length; count2++)
  {

  }

  newStudents = new Student[0];
  return newStudents;
 }

 public static Student[] getStudents()
 {
  Student[] students = new Student[]
  {
   new Student(7654321, "Lara Zhivago", 3.75),
   new Student(7654322, "Betty Brown", 1.9),
   new Student(7654323, "Chris Cross", 0.5),
   new Student(7654324, "Dr. Dre", 4.0),
   new Student(7654325, "Joe Cool", 2.0)
  };

  return students;
 }
}

/******************************************************************/
/******************************************************************/

class Student
{
  private int number;
  private String name;
  private double gpa;

  public Student(int snum, String sname, double sgpa)
  {
    this.number = snum;
    this.name = sname;
    this.gpa = sgpa;

  }

  public double getGPA()
  {
    return gpa;
  }

  public boolean isHonourStudent()
  {
    boolean isHonourStudent = false;
    if(getGPA() >= 3.5)
    {
      isHonourStudent = true;
    }
    return isHonourStudent;
  }

  public boolean isFailing()
  {
    boolean isFailing = false;
    if(getGPA() < 2.0)
    {
      isFailing = true;
    }
    return isFailing;
  }

  public String toString()
  {
    return  number + " " + name + " " + gpa;
  }
}

这是例外消息:

java.lang.NullPointerException
    at L2Q1.printStudents(L2Q1.java:41)
    at L2Q1.process(L2Q1.java:28)
    at L2Q1.main(L2Q1.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

3 个答案:

答案 0 :(得分:0)

这是因为您已经删除了部分学生,将他们从Student转为null。您正试图致电toString()的其中一位不存在的学生null

我建议您使用ArrayList替换数组,您可以从中实际删除元素。或者,您可以在删除学生时重建阵列。

答案 1 :(得分:0)

问题

您正在使students数组中的元素为null。然后,您尝试在toString()元素上使用null。这就是它的样子:null.toString(),显然这很糟糕。


没有ArrayList的解决方案

在评论中,人们建议ArrayList。我假设这是作业,你被迫使用数组,因此你应该这样做:

跟踪阵列的大小。使用它来确定列表中有多少个真实元素,非空元素。像这样:

for(int i = 0; i < arraySize; i++)
{
    // Do something with the array here.
}

请务必将arraySize声明为instance variable

private int arraySize = 0;

请记住,您需要为每个新元素增加arraySize,并在每次空元素时减少它。

请记住,当您的程序完成时,您的数组将在末尾有一个空值跟踪。


Hackish Solution

与此同时,您可以对printStudents()执行此操作 - 这是一种hackish,只是暂时的修复。

public static void printStudents(Student[] students)
{   
    // Check if the array is null, good practice.   
    if(students == null)
        return;

    System.out.println("Students:");

    for (int i = 0; i < students.length; i++)
    {   
        // My hack: Check if element is null, if it is then skip it.
        if(students[i] == null)
            continue;

        System.out.println(students[i].toString());
    }

    System.out.println();
}

答案 2 :(得分:0)

您正在将Student对象设置为null的索引,然后尝试在toString()对象上调用null的其他方法。使用List可以帮助您更优雅地解决此问题。

如果不重写整个代码,请按照以下方式实现List

public static void process() {
  List<Student> students = new ArrayList<Student>();
  List<Student> newStudents = new ArrayList<Student>();

...

然后当你在所有不同的函数中循环List时,你会使用

  for (int count = 0; count < students.size(); count++) {
    //do stuff here
  }

如果要从列表中删除学生,请使用

students.remove(count);

要填充列表,请使用

Student student1 = new Student(7654321, "Lara Zhivago", 3.75);
students.add(student1);
....