打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用Hashtable对字符串进行碰撞
使用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;
    }



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1261594

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java中字符串操作方法整理
java集合之BAT面试笔试
C#夯实基础系列之字符串
Stackoverflow上人气最旺的10个Java问题
为什么String被设计成不可变
ruby笔记
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服