免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4984 | 回复: 2
打印 上一主题 下一主题

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

论坛徽章:
0
1 [报告]
发表于 2004-09-02 02:14 |显示全部楼层

字符串转换的问题

真麻烦,用map不挺简单吗?

  1. public class CountedSet {

  2.     private Map sortMap;

  3.     /** Creates a new instance of CountedSet */
  4.     public CountedSet() {
  5.         sortMap = new TreeMap();
  6.     }

  7.     public void add(Object o)
  8.     {
  9.         int count;
  10.         if(sortMap.containsKey(o))
  11.         {
  12.             count = ((Integer)(sortMap.get(o))).intValue();
  13.             sortMap.put(o, new Integer(count+1));
  14.         }
  15.         else
  16.         {
  17.             sortMap.put(o, new Integer(1));
  18.         }
  19.     }
  20.     public int get(Object o)
  21.     {
  22.         Integer itg;
  23.         itg = (Integer)sortMap.get(o);
  24.         if(itg!=null)
  25.         {
  26.             return itg.intValue();
  27.         }
  28.         else
  29.         {
  30.             return 0;
  31.         }
  32.     }

  33.     public Map getMap()
  34.     {
  35.         return sortMap;
  36.     }
  37. }
复制代码


使用的时候

CountedSet cs = new CountedSet();

cs.add("a");
cs.add("b");
cs.add("a");

int a = cs.get("a"); // = 2
int b = cs.get("b"); // = 1
int c = cs.get("c"); // = 0

[ 本帖最后由 perryhg 于 2007-1-14 09:37 编辑 ]

论坛徽章:
0
2 [报告]
发表于 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
3 [报告]
发表于 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文档,只要你输入类名、函数名,马上就可以搜索到相关的类和函数,非常方便。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP