Today, the instructor talked about arrays. when sor () was mentioned, this sorting can be performed on arrays, so two problems emerged immediately in my mind: 1. What interfaces should be implemented if classes are sorted. 2. After implementing this interface, how does Java know whether a class has implemented an interface. So I searched for the problem.
For sorting of class arrays, call arrays. Sort (), but only supports basic types. If you sort classes, there are two methods:
Method 1: This class must implement the comparable <t> interface and implement the public int compareto (t o); method. If the result is greater than 1, 0 is returned, and-1 is returned. This interface implements generics. If declared, the compareto parameter is object.
Entity Student:
1 Public Class Student Implements Comparable < Student > {
2 Private String name = Null ;
3 Private Int Age = 0 ;
4 Public String getname (){
5 Return Name;
6 }
7 Public Void Setname (string name ){
8 This . Name = Name;
9 }
10 Public Int Getage (){
11 Return Age;
12 }
13 Public Void Setage ( Int Age ){
14 This . Age = Age;
15 }
16 Public Student (string name, Int Age ){
17 This . Name = Name;
18 This . Age = Age;
19 }
20 @ Override
21 Public String tostring (){
22 Return String. Format ( " Name = % s age = % d " , This . Name, This . Age );
23 }
24 @ Override
25 Public Int Compareto (student O ){
26 // Sort by name
27 Return This . Name. compareto (O. getname ());
28 }
29 }
Declare a student array and call arrays. Sort () to sort and then output
1 Student [] stus = New Student [ 3 ];
2 Stus [ 0 ] = New Student ( " Flowers " , 12 );
3 Stus [ 1 ] = New Student ( " Boys " , 13 );
4 Stus [ 2 ] = New Student ( " Zero " , 21 );
5 Arrays. Sort (stus );
6 For (Student s: stus ){
7 System. Out. println (S. tostring ());
8 }
Result:
Name = boys age = 13
Name = flowers age = 12
Name = zero age = 21
Method 2: If the student class does not implement the comparable <t> interface, arrays is called. when using sort (), you must specify a "Comparator" and an interface-class comparator <t>. Therefore, you must also write intcompare (T O1, t O2 );Code. The call code is as follows:
1 Arrays. Sort (stus, New Comparator < Student > (){
2 @ Override
3 Public Int Compare (student O1, student O2 ){
4 Return O1.getname (). compareto (o2.getname ());
5 }
6 });
For the arrangement of sets, such as arraylist, the collection <t> interface is implemented. List <t> inherits from collection <t>, so the implementation of list <t> is also applicable. Collection class sorting mainly uses the collections. sort method. Collections and collection are different. The former is a class, and the latter is an interface.
Generally, we mainly use two methods:
1. collection. Sort (list arg0 );
This is the simplest sorting method. You only need to implement its comparable interface and the Public int compareto (Object arg0) method.
1 Arraylist < Student > List = Newarraylist < Student > ( 3 );
2 List. Add ( New Student ( " Flowers " , 36 ));
3 List. Add ( New Student ( " Dog " , 23 ));
4 List. Add ( New Student ( " About " , 67 ));
5 Collections. Sort (list );
2. collection. Sort (list arg0, comparator arg1)
The comparator is added to facilitate management with greater flexibility. The comparator can be used as an internal static class for ease of management. The comparator must implement the comparator interface.
1 Collections. Sort (list, New Comparator < Student > (){
2 @ Override
3 Public Int Compare (student O1, student O2 ){
4 // Sort by age
5 Return O1.getage () > O2.getage () ? 1 :( O1.getage () = O2.getage () ? 0 : - 1 );
6 }
7 });
The above two methods have the same results:
Name = dog age = 23
Name = flowers age = 36
Name = about age = 67
ViewSource codeIt is not difficult to see the idea of Java. First, let's talk about converting the collection class into an array, then call the arrays. sort method for sorting, pass the comparator in the past, and finally assign the result to the Collection class using the set iterator.
1 Public Static < T > Void Sort (list < T > List, Comparator <? Super T > C ){
2 Object [] = List. toarray ();
3 Arrays. Sort (A, (comparator) C );
4 Listiterator I = List. listiterator ();
5 For ( Int J = 0 ; J < A. length; j ++ ){
6 I. Next ();
7 I. Set (A [J]);
8 }
9 }