As we all know, the bubble sort method is 3 steps in the general array,
1 if (a<b) {2 temp=A; 3 A=B; 4 b=temp; 5 }
However, it is not a simple swap in the collection, because after swapping, the new values must be guaranteed to be reset to the collection . So, is it hard? It's actually simpler:
1 if (a<b) {2 workerlist.get (j). SETSC (b); 3 Workerlist.get (j+1). SETSC (a); 4 }
Principle or exchange, but do not need the media temple.
See below for specific code:
Worker Class **********************
1 PackageCom.xtkj.worker;2 Public classworker{3 intID;4 String name;5 intAge ;6 Doublesalary;7 8 PublicWorker () {}//Construction Method9 PublicWorker (intId,string name,intAgeDoublesalary) {Ten This. ID =ID; One This. Name =name; A This. Age =Age ; - This. Salary =salary; - } the PublicString GetName () { - returnname; - } - Public voidsetName (String name) { + This. Name =name; - } + Public intgetId () { A returnID; at } - Public voidSetId (intID) { - This. ID =ID; - } - Public Doublegetsc () { - returnsalary; in } - Public voidSETSC (Doublesalary) { to This. Salary =salary; + } - Public intGetage () { the returnAge ; * } $ Public voidSetage (intAge ) {Panax Notoginseng This. Age =Age ; - } the + PublicWorker GetNext () { A return This. GetNext (); the } + -}
Method Class (only bubble sort is explained here) ***************
1 PackageCom.xtkj.worker;2 Importjava.util.ArrayList;3 Importjava.util.List;4 ImportJava.util.Scanner;5 Public classMethod {6 Public StaticList<worker> workerlist =NewArraylist<worker>();7 //Sort by salary from high to low8 Doubletemp;9 DoubleA=0;Ten DoubleB=0; One voidsort () { ASystem.out.println ("Sort results by employee pay from highest to lowest:"); - for(intI=0;i<workerlist.size () -1;i++){ - for(intJ=0;j<workerlist.size () -1-i;j++){ theA=Workerlist.get (j). GETSC (); -B=workerlist.get (j+1). GETSC (); - if(a<b) { -temp=A; + Workerlist.get (j). SETSC (b); Note: Here is the setting of the value to the collection. - //a=b; + Workerlist.get (j+1). SETSC (a);//Simple A and B swaps, which change only the results taken from the set, and the elements in the collection do not change. A //b=temp; at } - } - } -}
Interface Class (test Class) **********************
Package com.xtkj.worker;
Import Java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void Main (string[] args) {
Method method = new method ();
Boolean B = true;
while (b) {
System.out.println ();
SYSTEM.OUT.PRINTLN ("********************* Welcome to the staff salary management system ***********************");
SYSTEM.OUT.PRINTLN ("Please select the following actions:");
System.out.println ("1. Add new employee");
System.out.println ("2. Query employee Information");
System.out.println ("3. Show all employee Information");
System.out.println ("4. Modify employee's salary");
System.out.println ("5. Delete employee Information");
System.out.println ("6. Salary is ranked by low to High");
System.out.println ("7. Statistical staff average salary and minimum, maximum wage");
System.out.println ("8. Exit the system");
Scanner reader=new Scanner (system.in);
System.out.println ("Please enter function:");
int ab = Reader.nextint ();
Switch (AB)
{
Case 1:
Method.add ();
Break
Case 2:
Method.find ();
Break
Case 3:
Method.showall ();
Break
Case 4:
Method.updatesal ();
Break
Case 5:
Method.delete ();
Case 6:
Method.sort ();
Break
Case 7:
Method.statics ();
Break
Case 8:
System.out.println ("Retired Employee Payroll management System! ");
b = false;
}
}
Worker wr=new Worker ();
}
}
The results of the operation are as follows:
How to implement bubble sort in ArrayList in Java