国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開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

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java中字符串操作方法整理
java集合之BAT面試筆試
C#夯實基礎系列之字符串
Stackoverflow上人氣最旺的10個Java問題
為什么String被設計成不可變
ruby筆記
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服