PackageCom.swift;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List;ImportJava.util.Scanner; Public classtest2_objectio {@SuppressWarnings ("Unchecked") Public Static voidMain (string[] args) {/** 2nd: There are five students, each student has 3 courses (Chinese, maths, English) scores, write a program to receive information from the keyboard input students, * The input format is: name,30,30,30 (name, three lessons), and then the input of the student information according to the total score from the high To a low order written to a name "Stu.txt" file. * Requirements: Stu.txt file format to be more intuitive, open this file, you can see the student's information very clearly. */List<Student> list =NewArraylist<student>(); Scanner Scan=NewScanner (system.in); for(inti = 1; I <=5; i++) {System.out.println ("Please enter" +i+ "Student Information: format name 60 60 60"); String name=Scan.next (); Float Yuwen=scan.nextfloat (); Float Shuxue=scan.nextfloat (); Float Yingyu=scan.nextfloat (); Student Stu=NewStudent (name, Yuwen, Shuxue, Yingyu); List.add (Stu); } for(Student stu:list) {System.out.println (stu.tostring ()); } collections.sort (List,NewComparator<student>() {@Override Public intCompare (Student O1, Student O2) {floatNum= (O2.getshuxue () +o2.getyingyu () +o2.getyuwen ())-(O1.getshuxue () +o1.getyingyu () +O1.getyuwen ()); intI= (int) num; returni; } }); for(Student stu:list) {System.out.println (stu.tostring ()); } }}classStudent {PrivateString name; Private floatYuwen; Private floatShuxue; Private floatYingyu; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public floatGetyuwen () {returnYuwen; } Public voidSetyuwen (floatYuwen) { This. Yuwen =Yuwen; } Public floatGetshuxue () {returnShuxue; } Public voidSetshuxue (floatShuxue) { This. Shuxue =Shuxue; } Public floatGetyingyu () {returnYingyu; } Public voidSetyingyu (floatYingyu) { This. Yingyu =Yingyu; } PublicStudent (String name,floatYuwen,floatShuxue,floatYingyu) { Super(); This. Name =name; This. Yuwen =Yuwen; This. Shuxue =Shuxue; This. Yingyu =Yingyu; } PublicStudent () {Super(); } @Override PublicString toString () {return"Student [name=" + name + ", yuwen=" + Yuwen + ", shuxue=" + Shuxue + ", yingyu=" + Yingyu + "]"; }}
The comparator is an anonymous inner class, Collections.sort (List,comparator<student> () {});
The Compare method is then implemented, returning a value of type int, positive negative 0 representing greater than or equal to
Add the content code of IO stream as follows;
PackageCom.swift;ImportJava.io.BufferedWriter;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStreamWriter;Importjava.io.UnsupportedEncodingException;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List;ImportJava.util.Scanner; Public classtest2_objectio {@SuppressWarnings ("Unchecked") Public Static voidMain (string[] args) {/** 2nd: There are five students, each student has 3 courses (Chinese, maths, English) scores, write a program to receive information from the keyboard input students, * The input format is: name,30,30,30 (name, three lessons), and then the input of the student information according to the total score from the high To a low order written to a name "Stu.txt" file. * Requirements: Stu.txt file format to be more intuitive, open this file, you can see the student's information very clearly. */List<Student> list =initiatelist (); for(Student stu:list) {System.out.println (stu.tostring ()); } collections.sort (List,NewComparator<student>() {@Override Public intCompare (Student O1, Student O2) {floatNum= (O2.getshuxue () +o2.getyingyu () +o2.getyuwen ())-(O1.getshuxue () +o1.getyingyu () +O1.getyuwen ()); intI= (int) num; returni; } }); Initiate_io (list); } Private Static voidInitiate_io (list<student>list) {BufferedWriter bw; StringBuffer SB=NewStringBuffer (); for(Student stu:list) {sb.append (stu.tostring ()); Sb.append ("\ r \ n"); } Try{BW=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream ("Src\\stu.txt"), "Utf-8");//need to indicate pathBw.write (sb.tostring ()); Bw.flush (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } Private StaticList<student>initiatelist () {List<Student> list =NewArraylist<student>(); Scanner Scan=NewScanner (system.in); for(inti = 1; I <=5; i++) {System.out.println ("Please enter" +i+ "Student Information: format name 60 60 60"); String name=Scan.next (); Float Yuwen=scan.nextfloat (); Float Shuxue=scan.nextfloat (); Float Yingyu=scan.nextfloat (); Student Stu=NewStudent (name, Yuwen, Shuxue, Yingyu); List.add (Stu); } returnlist; }}classStudent {PrivateString name; Private floatYuwen; Private floatShuxue; Private floatYingyu; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public floatGetyuwen () {returnYuwen; } Public voidSetyuwen (floatYuwen) { This. Yuwen =Yuwen; } Public floatGetshuxue () {returnShuxue; } Public voidSetshuxue (floatShuxue) { This. Shuxue =Shuxue; } Public floatGetyingyu () {returnYingyu; } Public voidSetyingyu (floatYingyu) { This. Yingyu =Yingyu; } PublicStudent (String name,floatYuwen,floatShuxue,floatYingyu) { Super(); This. Name =name; This. Yuwen =Yuwen; This. Shuxue =Shuxue; This. Yingyu =Yingyu; } PublicStudent () {Super(); } @Override PublicString toString () {return"Student [name=" + name + ", yuwen=" + Yuwen + ", shuxue=" + Shuxue + ", yingyu=" + Yingyu + "]"; }}
The method of sorting the list collection in Java the use of the comparator is sorted according to the sum of the Chinese English scores of the students ' object sequence