2018-07-25-year Java sequencing program small case

Source: Internet
Author: User
Tags comparable

1, Student class

Package Cn.sjq.sort.java;

/**

* Sort student objects by implementing Java's comparable<t> interface

* This interface forces the overall ordering of objects for each class that implements it. This sort is called the natural sort of the class, and the CompareTo method of the class is called its natural comparison method

* The list of objects (and arrays) that implement this interface can be automatically sorted by Collections.sort (and Arrays.sort). An object that implements this interface can be used as a key in an ordered map table or as an element in an ordered collection without specifying a comparer.

* @author SONGJQ

*

*/

public class Student1 implements comparable<student1> {

Defining Student Object Properties

Private String Stuno;

Private String Stuname;

private int stuage;

private float Stumath;

Public String Getstuno () {

return Stuno;

}

public void Setstuno (String Stuno) {

This.stuno = Stuno;

}

Public String Getstuname () {

return stuname;

}

public void Setstuname (String stuname) {

This.stuname = Stuname;

}

public int getstuage () {

return stuage;

}

public void setstuage (int stuage) {

This.stuage = Stuage;

}

public float Getstumath () {

return stumath;

}

public void Setstumath (float stumath) {

This.stumath = Stumath;

}

Public Student1 () {

}

Public Student1 (String Stuno, string stuname, int stuage, float stumath) {

This.stuno = Stuno;

This.stuname = Stuname;

This.stuage = Stuage;

This.stumath = Stumath;

}

/*

* Re-student Object ToString () method

* (Non-javadoc)

* @see java.lang.object#tostring ()

*/

@Override

Public String toString () {

Return "[" +this.stuno+ "\ T" +this.stuname+ "\ T" +this.stuage+ "\ T" +this.stumath+ "]";

}

/*

* Compares the order of this object with the specified object. If the object is less than, equal to, or greater than the specified object, it returns a negative integer, 0, or a positive integer, respectively.

* Parameter: O-the object to compare.

Returns

* Negative integer, 0, or positive integer, depending on whether the object is less than, equal to, or greater than the specified object.

* Thrown: classcastexception-

* If the type of the specified object does not allow it to be compared with this object. (Non-javadoc)

*

* @see Java.lang.comparable#compareto (java.lang.Object)

*/

@Override

public int compareTo (Student1 o) {

This is sorted in descending order based on the math size

if (This.stumath<o.getstumath ()) {

return 1;

}else if (This.stumath = = O.getstumath ()) {

return 0;

}else {

return-1;

}

}

}

2, Javasortdemomain class

Package Cn.sjq.sort.java;

Import Java.util.Arrays;

/**

* Sort the collection of student objects

* @author SONGJQ

*

*/

public class Javasortdemomain {

public static void Main (string[] args) {

Instantiating a Student object

Student1 S1 = new Student1 ("STU000001", "Zhang", 89.12f);

Student1 s2 = new Student1 ("STU000005", "Lisi", 84.12f);

Student1 s3 = new Student1 ("STU000004", "Wanger", 81.22f);

Student1 S4 = new Student1 ("STU000003", "Mazi", 95.13f);

Student1 S5 = new Student1 ("STU000002", "Zhaowu", 86.12f);

/*

Method 1: Encapsulate the Student object to data, student object does not implement Comparable<t> interface

Student[] Stuarray = {s1, s2, S3, S4, S5};

Sort the Stuarray

Arrays.sort (Stuarray);

Output Stuarray Object collection

System.out.println ("************** arrays.sort (Stuarray) ********************");

for (Student Stu:stuarray) {

System.out.println (Stu);

}

*/

/**

* Operation Result:

* Student object does not implement Comparable<t> interface case, Executive report Cn.sjq.sort.java.Student cannot is cast to java.lang.Comparable

* Description If you want to use Arrays.sort (Stuarray) or Collections.sort (Stuarray) to sort the objects, you need to implement the Comparable<t> interface

*/

Method 2: Encapsulate the Student object to data, student object implementation Comparable<t> interface

Student1[] Stuarray = {s1, s2, S3, S4, S5};

Sort the Stuarray

Arrays.sort (Stuarray);

Output Stuarray Object collection

System.out.println ("************** arrays.sort (Stuarray) ********************");

for (Student1 Stu:stuarray) {

System.out.println (Stu);

}

/**

* Operation Result:

* [stu000003mazi2195.13]

[stu000001zhang1989.12]

[stu000002zhaowu1786.12]

[stu000005lisi1884.12]

[stu000004wanger2281.22]

As can be seen from the running results, it is true that the Stumath field is sorted in descending order

*/

}

}


2018-07-25-year Java sequencing program small case

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.