各位大侠帮我解决这道超级简单的问题(冒泡排序的) 从控制端舒服一个字符串 比如 4848adecb 然后排序后输出结果为4488abcde. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) { String values = "4848adecb"; char[] chars = new char[values.length()]; for (int i = 0; i < chars.length; i++) { chars[i] = values.charAt(i); } Arrays.sort(chars); String value = new String(chars); System.out.println(value); } public static String bubbleSort(String str){ char[] chars = new char[str.length()]; for(int i = 0 ; i < str.length() ; i++){ chars[i] = str.charAt(i); } for(int i = 0 ; i < chars.length ; i++){ for(int j = i + 1 ; j < chars.length ; j ++){ char temp; if(chars[i] > chars[j]){ temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } } } String retStr = ""; for(int i = 0 ; i < chars.length ; i ++){ retStr += chars[i]; } return retStr; } LZ这个是完整版的public class BubbleSortTest { public static String bubbleSort(String str){ char[] chars = new char[str.length()]; for(int i = 0 ; i < str.length() ; i++){ chars[i] = str.charAt(i); } for(int i = 0 ; i < chars.length ; i++){ for(int j = i + 1 ; j < chars.length ; j ++){ char temp; if(chars[i] > chars[j]){ temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } } } String retStr = ""; for(int i = 0 ; i < chars.length ; i ++){ retStr += chars[i]; } return retStr; } public static void main(String[] args) { System.out.println(bubbleSort(args.length > 0 ? args[0] : "kief351"));//如果控制端没有输入字符串就默认传一个 }} 何必这么麻烦嘛,jdk有对数组的操作类,干嘛不用? 求大神指导GridLayout与GridBagLayout的用法区别 JNative的内存不能够取出值,帮帮我 线性表取范围算法 extjs 和 Javascript各自的缺点 急 hibernate 双向一对一外键关联的问题!!!! BLOB的一个问题,请高手们解决一下 谁能举例简单说明使用j2ee和ejb的好处 使用spring报错Error getConfigured,求助大虾 javaEE初学者,有个很困惑的问题!望前辈指点迷津! 想请教一下关于Collection,Vector等集合类的问题! struts2流程简单问题(我是初学者) 权限问题
String values = "4848adecb";
char[] chars = new char[values.length()];
for (int i = 0; i < chars.length; i++) {
chars[i] = values.charAt(i);
}
Arrays.sort(chars);
String value = new String(chars);
System.out.println(value);
}
char[] chars = new char[str.length()];
for(int i = 0 ; i < str.length() ; i++){
chars[i] = str.charAt(i);
}
for(int i = 0 ; i < chars.length ; i++){
for(int j = i + 1 ; j < chars.length ; j ++){
char temp;
if(chars[i] > chars[j]){
temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
}
String retStr = "";
for(int i = 0 ; i < chars.length ; i ++){
retStr += chars[i];
}
return retStr;
}
public class BubbleSortTest { public static String bubbleSort(String str){
char[] chars = new char[str.length()];
for(int i = 0 ; i < str.length() ; i++){
chars[i] = str.charAt(i);
}
for(int i = 0 ; i < chars.length ; i++){
for(int j = i + 1 ; j < chars.length ; j ++){
char temp;
if(chars[i] > chars[j]){
temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
}
String retStr = "";
for(int i = 0 ; i < chars.length ; i ++){
retStr += chars[i];
}
return retStr;
}
public static void main(String[] args) {
System.out.println(bubbleSort(args.length > 0 ? args[0] : "kief351"));//如果控制端没有输入字符串就默认传一个
}
}