首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javascript 兑现 Hashtable ArrayList StringBuffer

2012-11-23 
javascript实现 Hashtable ArrayList StringBufferHashtableArrayList 并不是javascript 固有的类,但是我

javascript 实现 Hashtable ArrayList StringBuffer
Hashtable  ArrayList 并不是javascript 固有的类,但是我们可以模拟一个
下面的code用起来会是这样 :

var table = new HashTable();table.put(jsObjKey,jsObjValue);var arrayList = new ArrayList();arrayList.add(jsObj);...arrayList.size();


等等,相信看一下都能知道,这个模拟出来的Hashtable 和ArrayList 和java的十分相似,基本上在java里面怎么用的,在js里面我们就可以怎么用。


源代码:
// StringBuffer.function StringBuffer(){this.array = new Array();}StringBuffer.prototype.append = function (content){this.array[eval(this.array.length)] = content;}StringBuffer.prototype.toString  = function (){return this.array.join("");}// ArrayList.function ArrayList(){ this.index = -1; this.array = new Array(); }ArrayList.prototype.add = function (obj){this.index = this.index + 1;this.array[eval(this.index)] = obj;}ArrayList.prototype.get = function (index){return this.array[eval(index)];}ArrayList.prototype.size = function (){return this.index+1;}ArrayList.prototype.remove = function (index){var j = 0;var arrThis = this.array; var arrTemp = new Array();for(w=0;w<arrThis.length;w++){if (eval(index)!=eval(w)) {arrTemp[j] = arrThis[w];j++;}}this.array = arrTemp;this.index = eval(j-1); }// HashTable Object 注意哦,T 可是大些的function HashTable(){ this.arrValues = new ArrayList(); }function HashTable_Map(){var key = null;  var value = null; }HashTable.prototype.put = function (objKey,objValue){var isAdd = true;var arrThis = this.arrValues;for(i=0;i<arrThis.size();i++){var map = arrThis.get(i);if (map.key==objKey){map.value = objValue;isAdd = false;}}if (isAdd){var Map = new HashTable_Map();Map.key = objKey;Map.value = objValue;this.arrKeys = objKey;this.arrValues.add(Map);}}HashTable.prototype.get = function (objKey){var arrThis = this.arrValues;for(i=0;i<arrThis.size();i++){var map = arrThis.get(i);if (map.key==objKey) return map.value;}return null;}HashTable.prototype.keys = function (){var arrKeys = new Array();var arrThis = this.arrValues;for(i=0;i<arrThis.size();i++){var map = arrThis.get(i);arrKeys[i] = map.key;}return arrKeys;}HashTable.prototype.remove =  function (objKey){for(i=0;i<this.arrValues.size();i++){var map = this.arrValues.get(i);if (objKey == map.key){this.arrValues.remove(i);}}}




var table = new HashTable();table.put(jsObjKey,jsObjValue);var arrayList = new ArrayList();arrayList.add(jsObj);...arrayList.size();

等等,相信看一下都能知道,这个模拟出来的Hashtable 和ArrayList 和java的十分相似,基本上在java里面怎么用的,在js里面我们就可以怎么用。


源代码:
// StringBuffer.function StringBuffer(){this.array = new Array();}StringBuffer.prototype.append = function (content){this.array[eval(this.array.length)] = content;}StringBuffer.prototype.toString  = function (){return this.array.join("");}// ArrayList.function ArrayList(){ this.index = -1; this.array = new Array(); }ArrayList.prototype.add = function (obj){this.index = this.index + 1;this.array[eval(this.index)] = obj;}ArrayList.prototype.get = function (index){return this.array[eval(index)];}ArrayList.prototype.size = function (){return this.index+1;}ArrayList.prototype.remove = function (index){var j = 0;var arrThis = this.array; var arrTemp = new Array();for(w=0;w<arrThis.length;w++){if (eval(index)!=eval(w)) {arrTemp[j] = arrThis[w];j++;}}this.array = arrTemp;this.index = eval(j-1); }// HashTable Object 注意哦,T 可是大些的function HashTable(){ this.arrValues = new ArrayList(); }function HashTable_Map(){var key = null;  var value = null; }HashTable.prototype.put = function (objKey,objValue){var isAdd = true;var arrThis = this.arrValues;for(i=0;i<arrThis.size();i++){var map = arrThis.get(i);if (map.key==objKey){map.value = objValue;isAdd = false;}}if (isAdd){var Map = new HashTable_Map();Map.key = objKey;Map.value = objValue;this.arrKeys = objKey;this.arrValues.add(Map);}}HashTable.prototype.get = function (objKey){var arrThis = this.arrValues;for(i=0;i<arrThis.size();i++){var map = arrThis.get(i);if (map.key==objKey) return map.value;}return null;}HashTable.prototype.keys = function (){var arrKeys = new Array();var arrThis = this.arrValues;for(i=0;i<arrThis.size();i++){var map = arrThis.get(i);arrKeys[i] = map.key;}return arrKeys;}HashTable.prototype.remove =  function (objKey){for(i=0;i<this.arrValues.size();i++){var map = this.arrValues.get(i);if (objKey == map.key){this.arrValues.remove(i);}}}





很好的封装 2 楼 torycatkin 2008-12-22   我觉得这个和js 的Array没啥区别啊。。用Array做数据存储的容器。
我个人认为,可以写一个HashMap的对象,存储只用一个自定义的原型,比如:
function HashMap(){
   var cont = {};
   this.put = function(key,value){
     cont[key] = value;
  }
  this.get = function(key){
   return cont[key];
  }
}

热点排行