我尋找一種最簡單的方法來按照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("//////////////////////////");
}
}
@HristoTsonev試過任何給定的答案?討論答案對於找到解決問題的最佳方法非常重要。 – MChaker