2015-05-03 51 views
-3

我尋找一種最簡單的方法來按照float字段對數組中的對象進行排序。我可以通過Java中的float字段值對對象的基本數組進行排序嗎?

我有java.lang.NullPointerException當我調用方法sort();Main();

如何通過student[].Rating這些對象進行排序?任何想法? 這是我的課:

public class Students { 

public static String First_Name; 
public static String Last_Name; 
public static String id; 
public static String Spec; 
public static String Course; 
public static String Ratingstr; 
public float Rating; 
public static int Number_of_students; 
Students student[] = new Students[100]; 

public void sort() { 

    int j; 
    boolean flag = true; // set flag to true to begin first pass 
    Students temp; //holding variable 

    while (flag) { 
     flag = false; //set flag to false awaiting a possible swap 
     for (j = 0; j < student.length - 1; j++) { 
      if (student[ j].Rating < student[j + 1].Rating) // change to > for ascending sort 
      { 
       temp = student[ j];    //swap elements 
       student[ j] = student[ j + 1]; 
       student[ j + 1] = temp; 
       flag = true;    //shows a swap occurred 
      } 
     } 
    } 
} 

public void Read() { 
    try { 
     File fXmlFile = new File("C://Students.xml"); 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(fXmlFile); 
     //optional, but recommended 
     //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work 
     doc.getDocumentElement().normalize(); 

     //System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
     NodeList nList = doc.getElementsByTagName("student"); 
     System.out.println("----------------------------"); 

     for (int temp = 0; temp < nList.getLength(); temp++) { 

      Node nNode = nList.item(temp); 

      //System.out.println("\nCurrent Element :" + nNode.getNodeName()); 
      if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

       Element eElement = (Element) nNode; 
       System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); 
       System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); 
       System.out.println("Student id : " + eElement.getAttribute("id")); 
       System.out.println("Spec : " + eElement.getElementsByTagName("spec").item(0).getTextContent()); 
       System.out.println("Course : " + eElement.getElementsByTagName("course").item(0).getTextContent()); 
       System.out.println("Rating : " + eElement.getElementsByTagName("rating").item(0).getTextContent()); 

      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public void Read2() { 
    try { 
     File fXmlFile = new File("C://Students.xml"); 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(fXmlFile); 
     //optional, but recommended 
     //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work 
     doc.getDocumentElement().normalize(); 

     //System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
     NodeList nList = doc.getElementsByTagName("student"); 
     //System.out.println("----------------------------"); 

     for (Number_of_students = 0; Number_of_students < nList.getLength(); Number_of_students++) { 
      // Node nNode = nList.item(0); 
      Node nNode = nList.item(Number_of_students); 
      //System.out.println("\nCurrent Element :" + nNode.getNodeName()); 
      if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

       Element eElement = (Element) nNode; 

       student[Number_of_students] = new Students(); 

       student[Number_of_students].First_Name = eElement.getElementsByTagName("firstname").item(0).getTextContent(); 
       student[Number_of_students].Last_Name = eElement.getElementsByTagName("lastname").item(0).getTextContent(); 
       student[Number_of_students].Course = eElement.getElementsByTagName("course").item(0).getTextContent(); 
       student[Number_of_students].Ratingstr = eElement.getElementsByTagName("rating").item(0).getTextContent(); 
       student[Number_of_students].id = eElement.getAttribute("id"); 
       student[Number_of_students].Spec = eElement.getElementsByTagName("spec").item(0).getTextContent(); 

       System.out.println("----------------------------"); 

       student[Number_of_students].Rating = Float.parseFloat(student[Number_of_students].Ratingstr); 

       String Ratingstr = Float.toString(student[Number_of_students].Rating); 
       System.out.println("Rate is : " + Ratingstr); 
       System.out.println("First Name : " + First_Name); 
       System.out.println("Last Name : " + Last_Name); 
       System.out.println("Student id is : " + id); 
       System.out.println("Spec : " + Spec); 
       System.out.println("Course is : " + Course); 
       System.out.println("----------------------------"); 
       System.out.println("//////////////////////////"); 
      } 

     } 

    } catch (Exception e) { 
    } 

} 

public static void main(String argv[]) { 
    new Students().Read2(); 
    new Students().sort(); 

} 

public void print() { 

    System.out.println("----------------------------"); 

    // String Ratingstr = Float.toString(student[Number_of_students].Rating); 
    System.out.println("Rate is : " + Ratingstr); 
    System.out.println("First Name : " + First_Name); 
    System.out.println("Last Name : " + Last_Name); 
    System.out.println("Student id is : " + id); 
    System.out.println("Spec : " + Spec); 
    System.out.println("Course is : " + Course); 
    System.out.println("----------------------------"); 
    System.out.println("//////////////////////////"); 

} 

} 

回答

0

您正在使用的匿名對象的閱讀和整理,因此他們正在研究兩個不同的對象,

0

在你main方法是創建Students對象和讀取裏面的數據其字段Students[] student。之後,您將放棄此對象,創建新對象並嘗試對其空白的student數據進行排序。這樣會更好:

public static void main(String argv[]) { 
    Students s = new Students(); 
    s.Read2(); 
    s.sort(); 
} 

不用說,你的代碼是一團糟。

0

首先,讓我們來回顧你的代碼一點點:使用lambda表達式

製作類Student

class Student { 
    private String first_Name; 
    private String last_Name; 
    private String id; 
    private String spec; 
    private String course; 
    private float rating; 

    public Student(String first_Name, String last_Name,String id, 
        String spec,String course,float rating){ 

     this.first_Name=first_Name; 
     this.last_Name=last_Name; 
     this.id=id; 
     this.spec=spec; 
     this.course=course; 
     this.rating=rating; 

    } 

    // getters and setters 
    ... 
} 

然後在Students主類

public class Students { 

    private static Student students[] = new Student[100]; 


    public void Read() { 
     //your code here 
    } 

    public void Read2() { 
     try { 
      File fXmlFile = new File("C://Students.xml"); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(fXmlFile);umentElement().normalize(); 
      NodeList nList = doc.getElementsByTagName("student"); 

      for (int i = 0; i< nList.getLength(); i++) { 
      Node nNode = nList.item(i); 
      if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

       Element eElement = (Element) nNode; 

       students[i] = new Student 
       (          
        eElement.getElementsByTagName("firstname") 
        .item(0).getTextContent(), 

        eElement.getElementsByTagName("lastname") 
        .item(0).getTextContent(), 

        eElement.getAttribute("id"), 

        eElement.getElementsByTagName("spec") 
        .item(0).getTextContent(), 

        eElement.getElementsByTagName("course") 
        .item(0).getTextContent(), 

        Float.parseFloat 
        (eElement.getElementsByTagName("rating") 
        .item(0).getTextContent()) 

       ); 

       print(i); 
       } 
      } 
     } catch (Exception e) { 
      //stack trace 
     } 

    } 

    public static void main(String argv[]) { 
     Read2(); 
     students.sort((s1, s2) -> s1.getRating().compareTo(s2.getRating())); 

    } 

    public void print(int i) { 


      System.out.println("----------------------------"); 

      System.out.println("Rate is : " + students[i].getRate()); 
      System.out.println("First Name : " + students[i].getFirst_Name()); 
      System.out.println("Last Name : " + students[i].getLast_Name()); 
      System.out.println("Student id is : " + students[i].getId()); 
      System.out.println("Spec : " + students[i].getSpec()); 
      System.out.println("Course is : " + students[i].getCourse()); 
      System.out.println("----------------------------"); 
      System.out.println("//////////////////////////"); 
    } 
} 

所以,你可以排序的學生java 8

students.sort((s1, s2) -> s1.getRating().compareTo(s2.getRating())); 
+0

@HristoTsonev試過任何給定的答案?討論答案對於找到解決問題的最佳方法非常重要。 – MChaker

0

我猜它有關,因正在創建百個學生

Students student[] = new Students[100]; 

的數組,但你看不到百家事實。所以在數組中將會有null引用。在排序過程中,您可以嘗試對一名受過訓練的學生進行排序,而不是僅對已讀的學生進行排序。

相關問題