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);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值