ITeye博客

javascript 实现 Hashtable ArrayList StringBuffer

chrnc · 2008-11-18 12:53 · Web前端 · Markdown

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);
		}
	}
}

评论(2)

  1. #2 楼 · torycatkin · 2008-12-22 13:31

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

  2. #1 楼 · liushouzhao · 2008-12-12 19:55

    chrnc writes:

    Hashtable ArrayList 并不是javascript 固有的类,但是我们可以模拟一个 下面的code用起来会是这样 : [code=javascript] var table = new HashTable(); table.put(jsObjKey,jsObjValue);

    var arrayList = new ArrayList(); arrayList.add(jsObj);

    ... arrayList.size();

    [/code]

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

    源代码: [code=javascript] // 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); } } } [/code]

    很好的封装

评论功能维护中,当前仅展示历史评论。