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
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
相关文章推荐
- compareTo, compareToIgnoreCase,equals,==用法
- Java使用compareToIgnoreCase比较时间大小
- 白菜之compareTo()和compareToIgnoreCase()
- compareTo(String str)与compareToIgnoreCase(String str)
- String类的compareTo()和compareToIgnoreCase()
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- JAVA_TOJAVASE_Day05(数组 、数组排序、冒泡法和排序法)
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- java程序员从笨鸟到菜鸟之(二十八)集合TreeSet排序之compareTo和compare方法
- String的方法compareToIgnoreCase
- Java集合排序之compareTo&compare 接口 compareable & comparator
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解
- (十八)Java工具类StringUtils工具类compare、compareIgnoreCase方法详解