1. Sorting characters in a string
1 Packagecn.itcast_03;2 3 /*4 * Sort the characters in the string. 5 * Example: "DACGEBF"6 * Result: "ABCDEFG"7 * 8 * Analysis:9 * A: Define a stringTen * B: Convert the string to a character array One * C: Sort the character array A * D: Turn the sorted character array into a string - * E: Output the last string - */ the Public classArraytest { - Public Static voidMain (string[] args) { - //Define a string -String s = "DACGEBF"; + - //Convert a string to a character array + Char[] CHS =S.tochararray (); A at //sort a character array - Bubblesort (CHS); - - //Convert the sorted character array to a string -String result =string.valueof (CHS); - in //output the last string -SYSTEM.OUT.PRINTLN ("Result:" +result); to } + - //Bubble Sort the Public Static voidBubblesort (Char[] CHS) { * for(intx = 0; x < chs.length-1; X + +) { $ for(inty = 0; Y < chs.length-1-X; y++) {Panax Notoginseng if(Chs[y] > chs[y + 1]) { - Chartemp =Chs[y]; theChs[y] = chs[y + 1]; +Chs[y + 1] =temp; A } the } + } - } $}
Java Fundamentals Hardening 59: Case of sorting characters in a string