然而,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;
- }
- //实例化一个自定义的哈希表类
- 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的其它方法使用简单,读者可以自己测试 (此略)