//按照中文第一個字母升序排列的實(shí)現(xiàn)
創(chuàng)新互聯(lián)專注于蕉嶺網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供蕉嶺營銷型網(wǎng)站建設(shè),蕉嶺網(wǎng)站制作、蕉嶺網(wǎng)頁設(shè)計(jì)、蕉嶺網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造蕉嶺網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供蕉嶺網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ComparatorObject com=Collator.getInstance(java.util.Locale.CHINA);
String[] newArray={"上海","天津","廣州","杭州","遼寧","南京","武漢","北京","廈門","內(nèi)蒙"};
Arrays.sort(newArray,com);
for(String i:newArray){
System.out.print(i+" ");
}
}
}
import?java.util.Arrays;
import?java.util.Scanner;
//必須實(shí)現(xiàn)Comparable接口
public?class?Word?implements?ComparableWord{
private?final??String?word;
public?String?getWord()?{
return?word;
}
//構(gòu)造器什么的無視吧
public?Word(String?word)?{
if(word?==?null)
throw?new?NullPointerException("不可以創(chuàng)造空單詞!");
this.word?=?word;
}
//實(shí)現(xiàn)compareTo方法.主要的排序思路在這里
@Override
public?int?compareTo(Word?target)?{
if(target?==?null)
return?1;
if(target.getWord().equalsIgnoreCase(getWord()))
return?0;
char[]?selfLetters?=?getWord().toLowerCase().toCharArray();
char[]?targetLetters?=?target.getWord().toLowerCase().toCharArray();
int?selfLength?=?selfLetters.length;
int?targeLength?=targetLetters.length;
int?minLength?=?Math.min(selfLength,?targeLength);
for(int?index?=?0;index??minLength;index++){
if(selfLetters[index]??targetLetters[index]){
return?1;
}
else?if?(selfLetters[index]??targetLetters[index]){
return?-1;
}
continue;
}
return?selfLength??targeLength???1?:?-1?;
}
//重寫?ToString?方法以便打印輸出
@Override
public?String?toString()?{
return?word;
}
//主方法.用來查看效果
public?static?void?main(String[]?args)?{
?
int?size?=?5;//測試用的數(shù)組長度(單詞數(shù));
//?創(chuàng)造一個Word的數(shù)組用來保存輸入的單詞
Word[]?words?=??new?Word[size];
Scanner?sc?=?new?Scanner(System.in);
for(int?i=0;isize;i++)
{
System.out.println("請輸入第"+(i+1)+"個單詞");
words[i]?=?new?Word(sc.nextLine());
}
sc.close();//關(guān)閉流
System.out.println("排序結(jié)果為:");
//使用Arrays.sort方法排序,sort對自動調(diào)用你的compareTo方法來比較
Arrays.sort(words);
//打印出結(jié)果
System.out.println(Arrays.toString(words));
}
}
這是我剛寫的。測試結(jié)果還可以。邏輯可能不是很嚴(yán)謹(jǐn) 不過作為作業(yè)應(yīng)該應(yīng)付足夠了
package special7_2;
import java.util.*;
public class Special7_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="";
Scanner scan=new Scanner(System.in);
ArrayListString arraylist=new ArrayListString();
while(!str.equals("#")){
str=scan.nextLine();
arraylist.add(str);
}
arraylist.remove("#");
Object[]arr=arraylist.toArray();
for(int i=0;iarraylist.size()-1;i++){
//String str1=(String)arr[i];
for(int j=0;jarraylist.size()-1-i;j++){
if(((String) arr[j]).compareTo((String) arr[j+1])0){
//swap(arr,i,j);
String temp;
temp=(String) arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;iarraylist.size();i++){
//String str1=(String)arr[i];
System.out.println(arr[i]);
}
}
/*private static void swap(Object[] arra, int i, int j) {
// TODO Auto-generated method stub
String temp = (String) arra[i];
arra[i] = arra[j];
arra[j] = temp;
}*/
}