Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,我可以按浮点字段值对对象的基本数组进行排序吗?_Java_Arrays_Sorting_Object - Fatal编程技术网

在Java中,我可以按浮点字段值对对象的基本数组进行排序吗?

在Java中,我可以按浮点字段值对对象的基本数组进行排序吗?,java,arrays,sorting,object,Java,Arrays,Sorting,Object,我寻找一种最简单的方法,通过它们的浮点字段对数组中的对象进行排序 调用方法sort()时,我有java.lang.NullPointerException来自Main() 如何按学生[]对这些对象进行排序。评级?有什么想法吗? 这是我的班级: public class Students { public static String First_Name; public static String Last_Name; public static String id; public static

我寻找一种最简单的方法,通过它们的浮点字段对数组中的对象进行排序

调用方法
sort()时,我有
java.lang.NullPointerException
来自
Main()

如何按
学生[]对这些对象进行排序。评级
?有什么想法吗? 这是我的班级:

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

}

} 
公共班级学生{
公共静态字符串第一个\u名称;
公共静态字符串Last_Name;
公共静态字符串id;
公共静态字符串规范;
公共静态弦乐课程;
公共静态字符串比率STR;
公众浮动评级;
学生的公共静态整数;
学生[]=新生[100];
公共无效排序(){
int j;
boolean flag=true;//将flag设置为true开始第一次传递
Students temp;//保持变量
while(旗帜){
flag=false;//将flag设置为false,等待可能的交换
对于(j=0;j进行升序排序
{
temp=student[j];//交换元素
学生[j]=学生[j+1];
学生[j+1]=临时工;
flag=true;//显示发生了交换
}
}
}
}
公共无效读取(){
试一试{
File fXmlFile=新文件(“C://Students.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
//可选,但推荐
//请阅读以下内容-http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
//System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
NodeList nList=doc.getElementsByTagName(“学生”);
System.out.println(“-------------------------------”;
对于(int-temp=0;temppublic static void main(String argv[]) {
    Students s = new Students();
    s.Read2();
    s.sort();
}
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
   ...
}
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("//////////////////////////");
    }
}
students.sort((s1, s2) -> s1.getRating().compareTo(s2.getRating()));
Students student[] = new Students[100];