本文實(shí)例講述了Java實(shí)現(xiàn)按照大小寫(xiě)字母順序排序的方法。分享給大家供大家參考,具體如下:
這里排序需要得到的結(jié)果按字母順序。如:a-----z...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
import java.util.*; /** * 大小寫(xiě)字母的排序 * @author Administrator * */ public class z { //上邊是按大寫(xiě)在后的進(jìn)行排序 static Map<Character,Float> map= new HashMap<Character,Float>(); //hashMap允許null值 //按大寫(xiě)在前的進(jìn)行排序 static Map<Character,Float> map1= new HashMap<Character,Float>(); //hashMap允許null值 // static { Character ch; for (ch = 65 ; ch < 91 ; ch++) { //大寫(xiě)65.0;66.0;67.0;68.0 map.put(ch,( float )ch.hashCode()); } for (ch = 97 ; ch < 123 ; ch++) { //小寫(xiě)64.5;65.5 map.put(ch,( float )ch.hashCode()-( float ) 32 - 0 .5f); } for (ch= 65 ; ch < 91 ; ch++) { //大寫(xiě)65.0;66.0;67.0;68.0 map.put(ch, ( float )ch.hashCode()); } for (ch = 97 ; ch < 123 ; ch++) { //小寫(xiě)64.5;65.5 map.put(ch, ( float )ch.hashCode()-( float ) 32 + 0 .5f); } } /** * @first 大寫(xiě)在后的數(shù)組值在map中找 value的值 * @second 通過(guò)Collections的排序方法得到遞增數(shù)List * @third 反操作map得到char值 * @param ar要排序的數(shù)組 */ public List addList( char [] ar){ List list = new java.util.ArrayList(); for ( int i = 0 ; i < ar.length; i++) { list.add(map.get(ar[i])); } List lis= new ArrayList(); Collections.sort(list); Iterator it=list.iterator(); while (it.hasNext()){ String str=it.next().toString(); if (str.endsWith( ".0" )){ lis.add(( char )Float.parseFloat(str)); } else { lis.add(( char )(Float.parseFloat(str)+ 0.5 + 32 )); } } return lis; } /** * 大寫(xiě)在前的數(shù)組值在map中找value的值 * @param ar * @return */ public List addList1( char [] ar){ List list= new java.util.ArrayList(); for ( int i = 0 ; i < ar.length; i++) { list.add(map.get(ar[i])); } List lis= new ArrayList(); Iterator it=list.iterator(); while (it.hasNext()){ String str=it.next().toString(); if (str.endsWith( ".0" )){ lis.add(( char )Float.parseFloat(str)); } else { lis.add(( char )(Float.parseFloat(str)+ 0.5 - 32 )); } } return lis; } public static void main(String []args){ System.out.println( "服務(wù)器之家測(cè)試結(jié)果:" ); char ch [] ={ 'A' , 'a' , 'b' , 'f' , 'm' , 'K' }; List list= new z().addList(ch); Iterator it=list.iterator(); while (it.hasNext()){ System.out.println(it.next()+ "," ); } } } |
運(yùn)行結(jié)果:
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
原文鏈接:http://blog.csdn.net/lanchengxiaoxiao/article/details/7565806