String temp = str[b-1]; //str[b-1]当然只是一个String
str[b-1] = str[b];
str[b] = temp; 这么复杂做什么,直接使用Java本身的啦
java.util.Arrays.sort(str, new java.util.Compartor() {
public int compare(Object o1, Object o2) {
return ((String)o1).length() - ((String)o2).length();
}
});
str[b-1] = str[b];
str[b] = temp; 这么复杂做什么,直接使用Java本身的啦
java.util.Arrays.sort(str, new java.util.Compartor() {
public int compare(Object o1, Object o2) {
return ((String)o1).length() - ((String)o2).length();
}
});
这句话好像还有问题???
我刚开始接触,慢慢学的 呵呵
谢谢!
这句话没有问题
public static void main(String args[]) {
String str[] = {"what","is","yours","departments" };
int a, b;
int result;
// display original array
System.out.print("Original array is:");
for(int i=0; i < str.length; i++)
System.out.print(" " + str[i]);
System.out.println();
// This is the bubble sort.
for(a=1; a < str.length; a++)
for(b=str.length-1; b >= a; b--) {
result=str[b-1].compareTo(str[b]);
if(result>0) { // if out of order
// exchange elements
String temp = str[b-1]; //str[b-1]当然只是一个String
str[b-1] = str[b];
str[b] = temp; }
}
// display sorted array
System.out.print("Sorted array is:");
for(int i=0; i <str.length; i++)
System.out.print(" " + str[i]);
System.out.println();
}
}
修改过的程序
可是 输出不是按照排序阿?
public static void main(String args[]) {
// 需要被排序的String array
String str[] = {"what","is","yours","departments" };
String q[]; // 错误1,这里申明的是一个字符串数组,并没有对其成员进行初始化。
int a, b; // 告戒1,a、b变量做为循环计数器,不必事先申明,在利用C++或者JAVA时记得在需要他们的时候申明他们就可以了,这样可以避免名字域的误用以及释放大量无用空间。
int size; // What is it?
int result; // 同告戒1
size = 10; // number of elements to sort
// display original array
System.out.print("Original array is:");
for(int i=0; i < str.length; i++)
System.out.print(" " + str[i]);
System.out.println();
// This is the bubble sort.
for(a=1; a < str.length; a++) // 错误2,计数器a应从下标0开始,JAVA数组的第一个下标为0
for(b=str.length-1; b >= a; b--) { // 错误3,b > a,not b >= a
result=str[b-1].compareTo(str[b]); // Right
if(result>0) { // if out of order
// exchange elements
q[] = str[b-1]; // 接错误1,这里q应该改为一个字符串,而非使用字符串数组
str[b-1] = str[b];
str[b] = q[];
}
}
// display sorted array
System.out.print("Sorted array is:");
for(int i=0; i < size; i++) // 不明白,size在此地的引用为何???
System.out.print(" " + str[i]);
System.out.println();
}
}修改后的code
// Modification
class Bubble2 {
public static void main( String args[] ) {
String str[] = { "what", "is", "yours", "departments" };
int size = 10; // number of elements to sort,不清楚意义,保留
// display original array
System.out.print("Original array is:");
for( int i=0; i < str.length; i++ )
System.out.print( " " + str[i] );
System.out.println();
// This is the bubble sort.
for( int a=0; a < str.length - 1; a++ )
for( int b=str.length - 1; b > a; b-- ) {
int result = str[b-1].compareTo( str[b] );
if( result > 0 ) { // if out of order
// exchange elements
String q = str[b-1];
str[b-1] = str[b];
str[b] = q;
}
}
// display sorted array
System.out.print( "Sorted array is:" );
for( int i=0; i < str.length; i++ )
System.out.print( " " + str[i] );
System.out.println();
}
} /// @.@||~
排完序后 应该是 is what yours departments吧?
可是结果departments却在第一位?
demartments 的第一个字符是 d
is 的第一个字符是 i
what 的第一个字符是 w
yours的第一个字符是 y所以排序的顺序是 demartments is what yours如果希望以字符串中字符的个数进行排序就无法使用JDK自动给出的compareTo()方法了,你必须自己重新编写一个方法以判断两个字符串的大小。
@.@||~
result = str[b-1].length() - str[b].length();//一句话足够了!!!!!