有的方法还没经过测试,不知道有没有错误或者BUG,还有一些还没写完,有时间慢慢写上,有时间帮看看,找到错误一个10分,还可以追加。
function Vector(obj){
this.elements=new Array(obj);
}Vector.prototype={ elementsCount:0,//向量大小
/*
*兩種重載方式:add(obj)或add(obj,index)
*the first: 將指定元素追加到向量的末尾
*the second: 在此向量的指定位置插入指定的元素
*/
add:function(obj,index){
if(index==undefined){
this.elements[this.elementsCount++]=obj;
return true;
}
else{
for(var i=this.elementsCount;i>0&&i>index;i--)
this.elements[i]=this.elements[i-1];
this.elements[index]=obj;
this.elementsCount++;
}
},
/*
*兩種重載方式:addAll(objs)或addAll(objs,index)
*the first: 將指定objs中的所有元素追加到向量的末尾【按照集合的迭代順序追加元素】
*the second: 在指定位置將objs中的所有元素插入到此向量中
*/
addAll:function(objs,index){
if(index==undefined){
for(var i=0;i<objs.length;i++){
if(this.add(objs[i]))
continue;
return false;
}
}
else{
for(var i=this.elementsCount;i>0&&i>index;i--)
this.elements[i+objs.length-1]=this.elements[i-1];
this.elementsCount+=objs.length;
for(var i=0;i<objs.length;i++)
this.elements[index++]=objs[i];
}
},
//將指定的對象添加到向量的末尾,將其大小增加1
addElement:function(obj){
this.elements[this.elementsCount++]=obj;
},
//返回此向量的當前容量
capacity:function(){
return parseInt(this.elementsCount);
},
//從此向量中移除所有的元素
clear:function(){
this.elements.length=0;
this.elementsCount=0;
},
//深度複製
clone:function(){
var cloneObj=new Vector();
cloneObj.elements = this.elements.concat();
cloneObj.elementsCount=this.elementsCount;
return cloneObj;
},
//判斷向量中是否包含此元素
contains:function(obj){
return this.indexOf(obj)!=-1;
},
/*
*兩種重載方法:indexOf(obj) 或 indexOf(obj,index)
*the first: 搜索給定參數的第一個匹配項并返回索引如果搜索不到,則返回-1
*the second: 從索引index位置開始搜索
*/
indexOf:function(obj,index){
index=index==undefined?0:index;
for(var i=index;i<this.elementsCount;i++){
if(this.elements[i]!=obj)
continue;
return i;
}
return -1;
},
elementAt:function(index){
if(index>=this.size())
return;
return this.elements[index];
},
firstElement:function(){
return this.elements[0];
},
isEmpty:function(){
return this.elements.length==0;
},
lastElement:function(){
return this.elements[this.elements.length-1];
},
size:function(){
return this.elements.length;
},
setSize:function(newSize){
this.elementsCount=newSize;
},
containAll:function(c){
for(var elem in c){
if(this.indexOf(elem)!=-1)
continue;
return false;
}
return true;
},
copyInto:function(anArray){
for(var elem in this.elements)
anArray[anArray.length]=elem;
}
}
function Vector(obj){
this.elements=new Array(obj);
}Vector.prototype={ elementsCount:0,//向量大小
/*
*兩種重載方式:add(obj)或add(obj,index)
*the first: 將指定元素追加到向量的末尾
*the second: 在此向量的指定位置插入指定的元素
*/
add:function(obj,index){
if(index==undefined){
this.elements[this.elementsCount++]=obj;
return true;
}
else{
for(var i=this.elementsCount;i>0&&i>index;i--)
this.elements[i]=this.elements[i-1];
this.elements[index]=obj;
this.elementsCount++;
}
},
/*
*兩種重載方式:addAll(objs)或addAll(objs,index)
*the first: 將指定objs中的所有元素追加到向量的末尾【按照集合的迭代順序追加元素】
*the second: 在指定位置將objs中的所有元素插入到此向量中
*/
addAll:function(objs,index){
if(index==undefined){
for(var i=0;i<objs.length;i++){
if(this.add(objs[i]))
continue;
return false;
}
}
else{
for(var i=this.elementsCount;i>0&&i>index;i--)
this.elements[i+objs.length-1]=this.elements[i-1];
this.elementsCount+=objs.length;
for(var i=0;i<objs.length;i++)
this.elements[index++]=objs[i];
}
},
//將指定的對象添加到向量的末尾,將其大小增加1
addElement:function(obj){
this.elements[this.elementsCount++]=obj;
},
//返回此向量的當前容量
capacity:function(){
return parseInt(this.elementsCount);
},
//從此向量中移除所有的元素
clear:function(){
this.elements.length=0;
this.elementsCount=0;
},
//深度複製
clone:function(){
var cloneObj=new Vector();
cloneObj.elements = this.elements.concat();
cloneObj.elementsCount=this.elementsCount;
return cloneObj;
},
//判斷向量中是否包含此元素
contains:function(obj){
return this.indexOf(obj)!=-1;
},
/*
*兩種重載方法:indexOf(obj) 或 indexOf(obj,index)
*the first: 搜索給定參數的第一個匹配項并返回索引如果搜索不到,則返回-1
*the second: 從索引index位置開始搜索
*/
indexOf:function(obj,index){
index=index==undefined?0:index;
for(var i=index;i<this.elementsCount;i++){
if(this.elements[i]!=obj)
continue;
return i;
}
return -1;
},
elementAt:function(index){
if(index>=this.size())
return;
return this.elements[index];
},
firstElement:function(){
return this.elements[0];
},
isEmpty:function(){
return this.elements.length==0;
},
lastElement:function(){
return this.elements[this.elements.length-1];
},
size:function(){
return this.elements.length;
},
setSize:function(newSize){
this.elementsCount=newSize;
},
containAll:function(c){
for(var elem in c){
if(this.indexOf(elem)!=-1)
continue;
return false;
}
return true;
},
copyInto:function(anArray){
for(var elem in this.elements)
anArray[anArray.length]=elem;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货