对多个字符串进行排序,用Java语言实现,不能使用现有的类
2015-03-04 15:45
453 查看
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class StringCmp { static String[] strs = null; public static void order(String[] strs){ for(int i = 0 ; i < strs.length - 1; i++){ for(int j = 0 ; j < strs.length - 1 - i ; j++ ){ if(compare(strs[j],strs[j+1])){ exchange(j,j+1); } } } } public static boolean compare(String str1 , String str2){ char[] chars1 = str1.toCharArray(); char[] chars2 = str2.toCharArray(); int lenMin = Math.min(chars1.length, chars2.length); for(int i = 0 ; i < lenMin ; i++){ int charToSmall1 = (chars1[i] > 96)?chars1[i] - 96: chars1[i] - 64; int charToSmall2 = (chars2[i] > 96)?chars2[i] -96: chars2[i] - 64; if(charToSmall1 == charToSmall2){ if(chars1[i] > chars2[i]){ return true; }else if(chars1[i] < chars2[i]){ return false; }else{ continue; } }else{ if(charToSmall1 > charToSmall2){ return true; }else{ return false; } } } return false; } public static void exchange(int i , int j){ String tmp; tmp = strs[i]; strs[i] = strs[j]; strs[j] = tmp; } public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { strs = br.readLine().split(" "); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(String tmp : strs){ System.out.print(tmp + " "); } System.out.println(); order(strs); for(String tmp : strs){ System.out.print(tmp + " "); } } }
相关文章推荐
- *使用Java语言实现对ArrayList进行排序
- linq 扩展,在查询中使用字符串表达式对结果进行排序
- 【EntityFramework系列教程三,翻译】在ASP.NET MVC程序中使用EntityFramework对数据进行排序、过滤筛选以及实现分页
- 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数。
- 对字符串数组进行排序,在冒泡法排序中使用compareTo()方法确定排序的顺序。
- 【c语言】编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数
- 如何SQL Server 2005实现使用CLR函数实现字符串排序
- 实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构。 (即只使用基本的数据结构)
- 使用NSSortDescriptor对字符串数组进行排序
- 【C语言】编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数。
- 使用函数指针,完成一个sort()函数,能对任何类型的数组元素进行排序: 回调函数 以及 memcpy ()原型实现
- 实现C语言的拷贝函数且将复制后的字符串逆序,不能使用库函数,不能定义其他的变量。
- 【Java学习笔记】实现Comparator接口来进行字符串逆向排序
- 使用jquery.jqprint.js 实现的打印功能,IE9不能进行打印预览、火狐打印空白界面
- Java技巧——实现Comparator接口来进行字符串逆向排序
- 使用关键字对数组进行模糊查找;对一维字符串数组进行排序
- 对于集合中的自定义对象使用collections.sort 进行排序,需要实现compartor接口
- ajax返回的json内容进行排序使用sort()方法实现
- 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数
- 编写一个函数reverse_string(char * string)(递归实现)实现:将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数。