国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

散列表

2019-11-14 22:57:36
字體:
來源:轉載
供稿:網友
散列表

它是用一個散列函數把關鍵字 映射到散列表中的特定位置。 在理想情況下,如果元素e 的關鍵字為k,散列函 數為f,那么e 在散列表中的位置為f (k)。要搜索關鍵字為k 的元素,首先要計算出f (k),然后看 表中f (k)處是否有元素。如果有,便找到了該元素。如果沒有,說明該字典中不包含該元素。 在前一種情況中,如果要刪除該元素,只需把表中f (k)位置置為空即可。在后一種情況中,可 以通過把元素放在f (k)位置以實現插入。 此例是一個理想情況下的散列表,不考慮關鍵字重復的情況

public class HashList {            PRivate String[] table;    private int size;    private int threshold;    private static final int INITIAL_CAPACITY = 10;    private static final int SIZE_INCREASE = 10;    private static final float DEFAULT_LOAD_FACTOR = 0.75f;                public HashList(){        threshold = (int)(INITIAL_CAPACITY*DEFAULT_LOAD_FACTOR);        table = new String[INITIAL_CAPACITY];    }                public HashList(String[] table){        this.table = table;    }                    /**     * Get the actual size of the list     * @return     */    public int size(){        return size;    }        /**     * for test     * @return     */    public String[] getElements(){        return table;    }        public boolean contains(String element) {        if (element == null) {            return false;        }        if (element.equals(table[getIndex(element)])) {            return true;        }        return false;    }        public void add(String element){        int index = getIndex(element);        if(size>threshold){            resize();        }        table[index] = element;        size++;    }                //private methods    /**     * resize the array     */    private void resize(){        String[] newArray = new String[table.length+SIZE_INCREASE];        for(int i=0;i<table.length;i++){            newArray[i] = table[i];        }        table = newArray;        threshold = (int)(table.length*DEFAULT_LOAD_FACTOR);    }        /**     * get the index of the element     * @param element     * @return     */    public int getIndex(String element) {        return (element.hashCode() & 0x7FFFFFFF) % table.length;    }    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聂拉木县| 苍溪县| 惠来县| 永修县| 司法| 饶平县| 雅江县| 桂阳县| 苍南县| 龙口市| 南召县| 孟津县| 怀柔区| 丹棱县| 巩留县| 读书| 汉沽区| 达日县| 桐城市| 永年县| 桦南县| 察雅县| 南丹县| 土默特左旗| 沈丘县| 奇台县| 垦利县| 新绛县| 来安县| 芜湖市| 定安县| 兖州市| 陆丰市| 壤塘县| 手游| 神木县| 西畴县| 瓮安县| 刚察县| 贺州市| 汾阳市|