javascript中沒有像c#,java那樣的雜湊表(hashtable),
然而,javascript中的Array也只有一些類似於'雜湊表'的非常簡易功能, 如下:
- var arr = new Array();
- arr['item1'] = 'the value of item 1 ';
- arr['item2'] = 'the value of item 2 ';
- alert(arr['item1']);
- alert(arr['item2']);
但上述功能,不符我們的實際要求,另外查詢遍曆也不方便,我們需要在Array的基礎上進行擴充,
下面我們可以用js中的數組來實作類別似的hashtable的功能,
- function Hashtable(){
- this.clear = hashtable_clear;
- this.containsKey = hashtable_containsKey;
- this.containsValue = hashtable_containsValue;
- this.get = hashtable_get;
- this.isEmpty = hashtable_isEmpty;
- this.keys = hashtable_keys;
- this.put = hashtable_put;
- this.remove = hashtable_remove;
- this.size = hashtable_size;
- this.toString = hashtable_toString;
- this.values = hashtable_values;
- this.hashtable = new Array();
- }
- function hashtable_clear(){
- this.hashtable = new Array();
- }
- function hashtable_containsKey(key){
- var exists = false;
- for (var i in this.hashtable) {
- if (i == key && this.hashtable[i] != null) {
- exists = true;
- break;
- }
- }
- return exists;
- }
- function hashtable_containsValue(value){
- var contains = false;
- if (value != null) {
- for (var i in this.hashtable) {
- if (this.hashtable[i] == value) {
- contains = true;
- break;
- }
- }
- }
- return contains;
- }
- function hashtable_get(key){
- return this.hashtable[key];
- }
- function hashtable_isEmpty(){
- return (this.size == 0) ? true : false;
- }
- function hashtable_keys(){
- var keys = new Array();
- for (var i in this.hashtable) {
- if (this.hashtable[i] != null)
- keys.push(i);
- }
- return keys;
- }
- function hashtable_put(key, value){
- if (key == null || value == null) {
- throw 'NullPointerException {' + key + '},{' + value + '}';
- }else{
- this.hashtable[key] = value;
- }
- }
- function hashtable_remove(key){
- var rtn = this.hashtable[key];
- //this.hashtable[key] =null;
- this.hashtable.splice(key,1);
- return rtn;
- }
- function hashtable_size(){
- var size = 0;
- for (var i in this.hashtable) {
- if (this.hashtable[i] != null)
- size ++;
- }
- return size;
- }
- function hashtable_toString(){
- var result = '';
- for (var i in this.hashtable)
- {
- if (this.hashtable[i] != null)
- result += '{' + i + '},{' + this.hashtable[i] + '}\n';
- }
- return result;
- }
- function hashtable_values(){
- var values = new Array();
- for (var i in this.hashtable) {
- if (this.hashtable[i] != null)
- values.push(this.hashtable[i]);
- }
- return values;
- }
Hastable類使用方法:
- //執行個體化一個自訂的雜湊表類
- var hashTable = new Hashtable();
- hashTable.put(0,'abc'); //0為key, 'abc'為value
- hashTable.put(1,'123');
- hashTable.put(2,'88a');
- hashTable.put(3,'88a');
- //遍曆hashtable, 相當於c#和java中的foreach
- for (var key in hashTable.keys()){ /* 用keys方法 */
- alert(hashTable.get(key)); //按key遍曆value
- }
- //遍曆hashtable, 相當於c#和java中的foreach
- for (var key in hashTable.hashtable)){ /* 用hashtable屬性 */
- alert(hashTable.get(key)); //按key遍曆value
- }
- alert(hashTable.containsKey(1)); //返回true
- alert(hashTable.containsKey(4)); //因為不存在key為4的,返回false
alert(hashTable.containsValue('888')); //返回true
- alert(hashTable.containsValue('mobidogs')); //因為不存在value為'mobidogs'的,返回false
- hashTable.remove(1); //移除key為1的元素
- alert(hashTable.containsKey(1)); //因為key為1的元素已被上行的reomve()方法移除,所以返回false
- //關於hastable的其它方法使用簡單,讀者可以自己測試 (此略)