您的位置:首页 > 编程语言 > Java开发

Java中的冒泡排序及compareTo()和compareToIgnoreCase()的综合用法

2013-03-22 16:50 281 查看
public class SortString {

static String arr[] = {

"Now", "is", "the", "time", "for", "all", "good", "men",

"to", "come", "to", "the", "aid", "of", "their", "country"

};

public static void main(String args[]) {

for(int j = 0; j < arr.length; j++) {

for(int i = j + 1; i < arr.length; i++) {

if(arr[i].compareToIgnoreCase(arr[j]) < 0) {

String t = arr[j];

arr[j] = arr[i];

arr[i] = t;

}

/*

if(arr[i].compareTo(arr[j]) < 0) {

String t = arr[j];

arr[j] = arr[i];

arr[i] = t;

}

*/

}

System.out.println(arr[j]);

}

}

}

输出结果为:

compareToIgnoreCase( )

方法忽略大小写输出的结果是

aid

all

come

country

for

good

is

men

Now

of

the

the

their

time

to

to

compare()方法不忽略大小写

输出的结果为

Now

aid

all

come

country

for

good

is

men

of

the

the

their

time

to

to
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: