首页 > 开发 > JS > 正文

javascript函数库-集合框架

2024-09-06 12:42:34
字体:
来源:转载
供稿:网友
Classes:
Collections
Arrays
ArrayList
SortedList extends ArrayList
HashMap
HashSet
*/

/****************
Collections
NOTE:sort() return a new List
****************/
function Collections(){}
Collections.sort=function(){
if(arguments.length==1){
 var s=new SortedList();
 s.addAll(arguments[0]);
 return s;
}
else if(arguments.length==2){
 var s=new SortedList();
 s.setComparator(arguments[1]);
 s.addAll(arguments[0]);
 return s;
}
else 
 throw "IllegalArgument";
}
/***************
Arrays
****************/
function Arrays(){}
Arrays.asList=function(arr){
return new ArrayList(arr);
}


//ListIterator
function ListIterator(table,len){
   this.table=table;
this.len=len;                          
   this.index=0;
 
this.hasNext=function() {
 return this.index< this.len;
   }

   this.next=function() { 
 if(!this.hasNext())
  throw "No such Element!";
 return this.table[this.index++];
   }
}

/********************
ArrayList
********************/
function ArrayList(){
this.buffer=new Array();
if(arguments.length>0) this.buffer=arguments[0];
this.length=this.buffer.length;
}
ArrayList.prototype.hashCode=function(){
var h=0;
for(var i=0;i<this.lengh;i++)
 h+=this.buffer[i].hashCode();
return h;
}

ArrayList.prototype.size=function(){
return this.length;
}

ArrayList.prototype.clear=function(){
for(var i=0;i<this.length;i++) this.buffer[i]=null;
this.buffer.length=0;
this.length=0;
}

ArrayList.prototype.isEmpty=function(){
return this.length==0;
}

ArrayList.prototype.toArray=function(){
var copy=new Array();
for(var i=0;i<this.length;i++){
 copy[i]=this.buffer[i];
}
return copy;
}
ArrayList.prototype.get=function(index){
if(index>=0 && index<this.length)
 return this.buffer[index];
return null;
}

ArrayList.prototype.remove=function(param){
var index=0;
 
if(isNaN(param)){
 index=this.indexOf(param);
}
else index=param;
  
if(index>=0 && index<this.length){
 for(var i=index;i<this.length-1;i++)
  this.buffer[i]=this.buffer[i+1];
  this.length-=1;
  return true;
}
else return false;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表