javascript HashTable
来源:互联网 发布:中国网民数据 编辑:程序博客网 时间:2024/06/15 11:45
//哈希表function HashTable() { //存数据的实体 this._Hash = new Object(); //新增键值 this.Add = function (key, value) { if (typeof (key) != "undefined") { if (this.Contains(key) == false) { this._Hash[key] = value; return true; } else { return false; } } else { return false; } } //删除键值 this.Remove = function (key) { delete this._Hash[key]; } //数量 this.Count = function () { var i = 0; for (var k in this._Hash) { i++; } return i; } //获取值 this.Items = function (key) { var value = this._Hash[key]; return typeof (value) == "undefined" ? null : value } //获取值 this.GetValue = function (key) { return this.Items(key); } //获取值 this.Get = function (key) { return this.Items(key); } //赋值 this.SetValue = function (key, value) { return this._Hash[key] = value; } //是否包含键 this.Contains = function (key) { return typeof (this._Hash[key]) != "undefined"; } //清空哈希表 this.Clear = function () { for (var k in this._Hash) { delete this._Hash[k]; } } //获取所有键 this.AllKeys = function () { return this._Hash; }}
新建: var ht = new HashTable();
添加: ht.Add("kk","hello");
遍历:
<pre class="prettyprint javascript" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;"><span style="white-space:pre"></span>for</span></span> (<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">var</span></span> i = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span>; i < <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">6</span></span>; i++<span style="color: rgb(0, 0, 0);">) { <span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>tmpEmployee </span>= <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">new</span></span> employee(i, <span style="color: rgb(128, 0, 0);"><span class="string" style="color: rgb(221, 17, 68);">"</span></span><span class="string" style="color: rgb(221, 17, 68);"><span style="color: rgb(128, 0, 0);">Employee_</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span> +<span style="color: rgb(0, 0, 0);"> i); <span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>ht.Add(i, tmpEmployee); <span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>} <span class="indent"> </span><span class="indent"> </span><span class="indent"> </span></span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">for</span></span> (<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">var</span></span> i = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span>; i <= ht.Count; i++<span style="color: rgb(0, 0, 0);">) { <span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>alert(ht.GetValue(i).userName); </span><span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">其实等价于ht.ObjArr[i].userName </span></span><span style="color: rgb(0, 128, 0);"><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span></span><span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">alert(ht.ObjArr[i].userName); </span></span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>} </span>
for (var i = 1; i <= ht.Count; i++) { alert(ht.GetValue(i)); //其实等价于ht.ObjArr[i].userName }
参考:http://www.tuicool.com/articles/67NVzq
0 0
- JAVASCRIPT HashTable
- javascript hashtable
- javascript HashTable
- javascript 实现hashtable集合
- javascript Ajax And hashtable
- Javascript Hashtable 类
- javascript的hashtable实现
- javascript hashtable例子
- Javascript实现HashTable(哈希表)
- javascript实现HashTable
- javascript中的哈希表(hashtable)实现
- javascript实现HashTable(完整版)
- [Javascript Data Structures] HashTable 哈希表
- shawl.qiu Javascript 哈希表类 / HashTable v1.0
- 一个用javascript写的hashtable类
- 【JavaScript】在JS中使用哈希表(hashTable)
- 哈希表(hashtable)的javascript简单实现
- javascript 哈希表(hashtable)的简单实现
- leetcode之jump game II
- 【机房重构】 数据类型转换
- Java解析Json数据
- EditText
- 《leetCode》:Excel Sheet Column Number
- javascript HashTable
- java web 加载配置文件信息
- android问题(1)
- iframe 设定高度与宽度
- VM虚拟机上linux连网问题
- aoj-733-闭合折线
- String、StringBuffer、StringBuilder
- Rt3070无线网卡AP功能移植到s5pv210
- AC自动机