使用Hashtable对字符串进行碰撞

1.在一些字符串数组中,常会有重复的记录,比如手机号码,我们可以通过Hashtable来对其进行过滤

public String[] checkArray(String[] str)...{
Hashtable<String, String> hash=new Hashtable<String, String>();

for(int i=0;i<str.length;i++)...{
if(!hash.containsKey(str[i])) //测试指定对象是否为此哈希表中的键
hash.put(str[i], str[i]);
}

Enumeration enumeration=hash.keys(); //返回此哈希表中的键的枚举
String[] str_new=new String[hash.size()];
int i=0;

while(enumeration.hasMoreElements())...{
str_new[i]=enumeration.nextElement().toString();
i++;
}
return str_new;
}
示例:
String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};
mobile=checkArray(mobile);
for(int i=0;i<mobile.length;i++)
System.out.println(mobile[i]);
输出结果为:
13811071503
13811071501
13811071500

  2.A,B均为字符串数组,找出在A中存在,而在B中不存在的字符串
public String[] compareArray(String[] A,String[] B){
Hashtable<String, String> hash=new Hashtable<String, String>();
Hashtable<String, String> hash_new=new Hashtable<String, String>();

for(int i=0;i<B.length;i++)
hash.put(B[i], B[i]);

for(int i=0;i<A.length;i++){
if(!hash.containsKey(A[i]))
hash_new.put(A[i], A[i]);
}

String[] C=new String[hash_new.size()];
int i=0;
Enumeration enumeration=hash_new.keys();

while(enumeration.hasMoreElements()){
C[i]=enumeration.nextElement().toString();
i++;
}
return C;
}
示例:
String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};
String[] mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};
String[] mobile3=compareArray(mobile1,mobile2);
for(int i=0;i<mobile3.length;i++)
System.out.println(mobile[i]);
输出结果:
13811071503
13811071501
存在的问题:
每次都是倒序,可以再对程序稍加改动,变成正序。

  3.将一个字符串数组中某一个特定的字符串过滤掉

/** *//**检验一个字符串数组,若包含某一特定的字符串,则将该字符串从数组中删
除,返回剩余的字符串数组
* @param str_array 字符串数组
* @param str_remove 待删除的字符串
* @return 过滤后的字符串
*/
public String[] removeStrFromArray(String[] str_array,String
str_remove)...{
Hashtable<String, String> hash=new Hashtable<String, String>();
for(int i=0;i<str_array.length;i++)...{
if(!str_array[i].equals(str_remove))
hash.put(str_array[i], str_array[i]);
}
//生成一个新的数组
String[] str_new=new String[hash.size()];
int i=0;
Enumeration enumeration=hash.keys();
while(enumeration.hasMoreElements())...{
str_new[i]=enumeration.nextElement().toString();
i++;
}
return str_new;
}

posted @ 2007-07-30 16:36 my 阅读(1336) | 评论 (0)编辑 收藏

7月22日和朋友在电脑城,本想只是去看看,下个月再出手,但是还是抵挡不住这款本子那诱人的价格和JS的吹嘘。看来自己还需要多磨练啊~~幸运的是,华硕这款本子用到现在基本没什么问题,上网,看文档,敲些代码还是可以的。玩游戏,实况足球10也都还可以玩得起,自己满足了呵呵。4月份买的台式机早就被占用了,现在终于可以专心看点东西了。
上图:

单独照:

posted @ 2007-07-27 08:20 my 阅读(1709) | 评论 (0)编辑 收藏

7月26号,到这家公司整整一年了。走与不走还在斗争!
发生了几件事情
1.公司的人事经理被开掉了,据说是老大直接开走的,对她的工作确实没有值得满意的地方,另外一些拿着不 低的薪水,而后不认真的同事也被干掉了,有一个人还能天天看小说。。晕
2.有很多人来面试,估计公司要招几个开发的充实我们
3.自己买了款不是很贵的本子,以后在上图了~
4.发现楼下那家快餐店做的饭菜很好吃,这样午饭省了不少心,5元/份,四个荤菜,满足了。。。还可以在户外吃,强悍
5.发现对面公司是做建筑软件的,吃饭都不出门,,,强。。但每天都在招人。。(不过MM很多,也很PP)
6.需要看书,<JAVA与模式>,<深入浅出Tapestry>, <JDK6学习笔记>


posted @ 2007-07-26 17:27 my 阅读(806) | 评论 (0)编辑 收藏

14号搬的家,16号就开始走路的去上班了,算过也要15分钟,麻烦的是过马路要等车,附近的人行天桥要走10分钟这样,只能放弃。早上打算吃米粉,但是都睡到很晚(丢脸...),还是继续着面包豆奶的日子。以后上班舒服多了,附近就是一个公园。周末偶尔去游泳,,,希望在新的地方有新的收获多些。。。

posted @ 2007-07-18 15:16 my 阅读(790) | 评论 (0)编辑 收藏

霉翻了的7月份,从来没有这么强烈希望时间消逝
见鬼去吧,7月


放松心情,多去锻炼。。。

posted @ 2007-07-09 16:42 my 阅读(764) | 评论 (0)编辑 收藏

仅列出标题
共13页: 上一页 1 2 3 4 5 6 7 8 9 下一页 Last 

posts - 63, comments - 45, trackbacks - 0, articles - 99

Copyright © my