免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: teron
打印 上一主题 下一主题

字符串转换的问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2004-09-02 15:24 |只看该作者

字符串转换的问题

中文问题就是指读中文文本 ,那段代码是别人给的,我也是那一段看不太懂。不知道哪位可以看懂。好像是把中文转换成那样的。不过好像放中文用的是char[][]数组,放英文用的是String[][]数组。但是中英文混合在一起都没法解决,现在要找一个方法搞定这两种情况。英文的符号问题好解决,中文的哪些,。、?我不知道如何解决。

论坛徽章:
0
12 [报告]
发表于 2004-09-02 16:05 |只看该作者

字符串转换的问题

你的需求是什么?
只有中文的字才处理?我对字符编码不是很熟悉,不知道那段代码把哪些字符替换成空格了。

论坛徽章:
0
13 [报告]
发表于 2004-09-02 16:29 |只看该作者

字符串转换的问题

我最终的目的是要处理中英文混合的文本。可现在中文还没有搞定:
字符编码我也不太熟,有个哥们发过来了这样一段:

中文字符,你可以用下面的语句获得:
System.out.println(Integer.toHexString((int)('?')));//待输出字符为中文字符

论坛徽章:
0
14 [报告]
发表于 2004-09-03 05:35 |只看该作者

字符串转换的问题

  1. public class StringCount {
  2.    
  3.     /**
  4.      * 锁定属性 string 的值。
  5.      */
  6.     private String string;
  7.    
  8.     /**
  9.      * 锁定属性 count 的值。
  10.      */
  11.     private int count;
  12.    
  13.     /** Creates a new instance of StringCount */
  14.     public StringCount(String string, int count) {
  15.         this.string = string;
  16.         this.count = count;
  17.     }
  18.    
  19.     /**
  20.      * 属性 string 的获取方法。
  21.      * @return 属性 string 的值。
  22.      */
  23.     public String getString() {
  24.         return this.string;
  25.     }
  26.    
  27.     /**
  28.      * 属性 string 的设置方法。
  29.      * @param string 属性 string 的新值。
  30.      */
  31.     public void setString(String string) {
  32.         this.string = string;
  33.     }
  34.    
  35.     /**
  36.      * 属性 count 的获取方法。
  37.      * @return 属性 count 的值。
  38.      */
  39.     public int getCount() {
  40.         return this.count;
  41.     }
  42.    
  43.     /**
  44.      * 属性 count 的设置方法。
  45.      * @param count 属性 count 的新值。
  46.      */
  47.     public void setCount(int count) {
  48.         this.count = count;
  49.     }
  50.    
  51. }
复制代码

  1. import java.util.*;

  2. public class CountedSet {
  3.     private Map sortMap;
  4.     public static final Comparator WORD_COUNT_ORDER
  5.                                      = new WordCountComparator();
  6.     private static class WordCountComparator
  7.                          implements Comparator, java.io.Serializable {

  8.         public int compare(Object o1, Object o2) {
  9.             StringCount s1 = (StringCount) o1;
  10.             StringCount s2 = (StringCount) o2;
  11.             int n1=s1.getCount(), n2=s2.getCount();
  12.             return n1 - n2;
  13.         }
  14.     }

  15.     /** Creates a new instance of CountedSet */
  16.     public CountedSet() {
  17.         sortMap = new TreeMap();
  18.     }

  19.     public void add(Object o)
  20.     {
  21.         int count;
  22.         if(sortMap.containsKey(o))
  23.         {
  24.             count = ((Integer)(sortMap.get(o))).intValue();
  25.             sortMap.put(o, new Integer(count+1));
  26.         }
  27.         else
  28.         {
  29.             sortMap.put(o, new Integer(1));
  30.         }
  31.     }
  32.     public int count(Object o)
  33.     {
  34.         Integer itg;
  35.         itg = (Integer)sortMap.get(o);
  36.         if(itg!=null)
  37.         {
  38.             return itg.intValue();
  39.         }
  40.         else
  41.         {
  42.             return 0;
  43.         }
  44.     }

  45.     public List getCountSet()
  46.     {
  47.         String word;
  48.         StringCount sc;
  49.         int count;
  50. //        Set countSortSet = new TreeSet(WORD_COUNT_ORDER);
  51.         List countList = new ArrayList();
  52.         Set keySet = sortMap.keySet();
  53.         Iterator it = keySet.iterator();
  54.         while(it.hasNext())
  55.         {
  56.             word = (String)it.next();
  57.             count = ((Integer)sortMap.get(word)).intValue();
  58.             sc = new StringCount(word, count);
  59.             countList.add(sc);
  60.         }
  61.         Collections.sort(countList, WORD_COUNT_ORDER);
  62.         return countList;
  63.     }
  64. }
复制代码


在main里面这样调用
  1.         CountedSet cs = new CountedSet();
  2.         cs.add("b");
  3.         cs.add("b");
  4.         cs.add("c");
  5.         cs.add("a");
  6.         cs.add("b");
  7.         cs.add("a");
  8.         cs.add("你");
  9.         cs.add("好");
  10.         cs.add("你");
  11.         System.out.println("a "+cs.count("a"));
  12.         System.out.println("b "+cs.count("b"));
  13.         System.out.println("c "+cs.count("c"));
  14.         System.out.println("d "+cs.count("d"));
  15.         System.out.println("你 "+cs.count("你"));
  16.         System.out.println("以下结果为升序排序结果");
  17.         List ss = cs.getCountSet();
  18.         Iterator it = ss.iterator();
  19.         while(it.hasNext())
  20.         {
  21.             StringCount stc = (StringCount)it.next();
  22.             System.out.println(stc.getString()+" "+stc.getCount());
  23.         }
复制代码


得到结果为:
a 2
b 3
c 1
d 0
你 2
以下结果为升序排序结果
c 1
好 1
a 2
你 2
b 3

论坛徽章:
0
15 [报告]
发表于 2004-09-03 08:30 |只看该作者

字符串转换的问题

perryhg对Collection Framework很熟悉啊。
要经常来java版支持一下啊

论坛徽章:
0
16 [报告]
发表于 2004-09-03 09:25 |只看该作者

字符串转换的问题

是呀,惊叹!!perryhg用到的一些函数我只有一个一个的去查才知道。我要降序排列,可我不知道如何修改Collections.sort(countList, WORD_COUNT_ORDER); 的第二个参数。

论坛徽章:
0
17 [报告]
发表于 2004-09-03 17:44 |只看该作者

字符串转换的问题

咋没有人来应一声了呀!

论坛徽章:
0
18 [报告]
发表于 2004-09-04 05:44 |只看该作者

字符串转换的问题

要改变顺序有好多方法啊首先,我的Comparator类里面
  1.     public static final Comparator WORD_COUNT_ORDER
  2.                                      = new WordCountComparator();
  3.     private static class WordCountComparator
  4.                          implements Comparator, java.io.Serializable {

  5.         public int compare(Object o1, Object o2) {
  6.             StringCount s1 = (StringCount) o1;
  7.             StringCount s2 = (StringCount) o2;
  8.             int n1=s1.getCount(), n2=s2.getCount();
  9.             return n1 - n2;
  10.         }
  11.     }
复制代码


把最后一行改成 return n2-n1; 就可以实现降序排列了。
另外,Collections.sort(list, Collections.reverseOrder()); 就可以实现list里面的顺序逆向排序,还有
那个list如果你要倒序的话,只要从最后一项开始取不就行了?

for(int i=list.size();i>;0;i--)
{
    StringCount sct = (StringCount) list.get(i);
}
要灵活应用的,不能死抱着sort不放啊。
我以前没有学过数据结构,后来自己买了本《数据结构和Java Collection Framework》看,慢慢摸索的。不过最好的办法还是多看文档,建议下载chm格式的java文档,只要你输入类名、函数名,马上就可以搜索到相关的类和函数,非常方便。

论坛徽章:
0
19 [报告]
发表于 2004-09-05 12:24 |只看该作者

字符串转换的问题

关键是理解一点,真正影响两个对象比较结果的其实是被排序的对象本身,而不是Collection。这就是面向对象的设计思想的表现啊。一个通用的排序程序只是负责从一个集合里如何取出对象,如何把排序的结果存储,以及如何使用户方便快捷的得到排序的结果。
而真正比较大小的事情其实就是通知两个对象:“你们可以比较大小了。你们俩谁大?”,然后听一下他们俩的答案。仅此而已。这样不论使比较什么类型的对象,只要这些对象实现了Comparable接口就可以了。

论坛徽章:
0
20 [报告]
发表于 2004-09-06 08:35 |只看该作者

字符串转换的问题

向perryhg 和sakulagi再次表示感谢!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP