发现了jdk里面一个奇怪的写法!

tojsp_com 发布于 2010/12/29 11:18
阅读 846
收藏 4
JDK
package com.compare;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ST {
	
	private final static Cp CP=new Cp();

	public static void main(String[] args) {
		
		String s1="aB.c@D/";
		String s2="Ab.a@D/";

		List ss=new ArrayList();
		ss.add(s1);
		ss.add(s2);
		
		Collections.sort(ss,CP);
		
		for(String s:ss){
			System.out.println(s);
		}
	}
	
	static class Cp implements Comparator{
		public int compare(String s1, String s2) {
			int len1=s1.length(),len2=s2.length();
			for(int i1=0,i2=0;i1<len1&&i2<len2;i1++,i2++){
				char c1=s1.charAt(i1);
				char c2=s2.charAt(i2);
				if(c1!=c2){
					c1=Character.toUpperCase(c1);
					c2=Character.toUpperCase(c2);
					if (c1 != c2) {
	                    c1 = Character.toLowerCase(c1);
	                    c2 = Character.toLowerCase(c2);
	                    if (c1 != c2) {
	                        return c1-c2;
	                    }
	                }
				}
			}
			return len1-len2;
		}
	}
}

这是一个比忽略大小写的一个排序算法,把源码给拿出来了,有一点看不懂

把字符转成大写比较之后,什么情况出现不相等呢?

还要再转一次小写比较?这个地方有点不懂!

JDK里面的代码是String 类,里面line:1209

public static final Comparator CASE_INSENSITIVE_ORDER= new CaseInsensitiveComparator();
加载中
0
红薯
红薯

这个问题有点意思,代码是出现在 JDK 的源码里哪个类啊?

0
tojsp_com
tojsp_com

JDK里面的代码是String 类,里面line:1209

0
蔡华江
蔡华江

regionMatches方法里有说明

0
郑雨涵
郑雨涵

等于0才相等,不等于不相等

0
xianlai
xianlai

我也迷糊着……

0
tojsp_com
tojsp_com

引用来自#5楼“xiaojia2008”的帖子

等于0才相等,不等于不相等

 兄弟,你根本就没看什么问题。。

0
tojsp_com
tojsp_com

引用来自#4楼“蔡华江”的帖子

 

regionMatches方法里有说明

 兄弟果然眼细,原来是sun自己的代码不行,所以要比较两次,哈哈。。原来如此啊 

 Unfortunately, conversion to uppercase does not work properly

if (ignoreCase) {
                // If characters don't match but case may be ignored,
                // try converting both characters to uppercase.
                // If the results match, then the comparison scan should
                // continue.
                char u1 = Character.toUpperCase(c1);
                char u2 = Character.toUpperCase(c2);
                if (u1 == u2) {
                    continue;
                }
                // Unfortunately, conversion to uppercase does not work properly
                // for the Georgian alphabet, which has strange rules about case
                // conversion.  So we need to make one last check before
                // exiting.
                if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
                    continue;
                }
            }
0
tojsp_com
tojsp_com

具体原因在这

Note that Character.isUpperCase(Character.toUpperCase(codePoint)) does not always return true for some ranges of characters, particularly those that are symbols(符号) or ideographs(象形文字).

In general, java.lang.String.toUpperCase() should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods. String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case mapping

建议使用string.toUpperCase , have serveral benefits ..

0
xianlai
xianlai

难道两次就保险了吗?

0
星星王
星星王

effect java这本书里有讲这个问题

返回顶部
顶部