怎样用javascript实现java中vector的功能,可否给出代码?谢谢

crinoid 2003-10-17 02:06:18
同上
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
crinoid 2003-10-17
  • 打赏
  • 举报
回复
呵呵,我找到一个比较能满足我要求的Vector.js
各位看看:
function Vector(cap)
{
this.elementData = new Array(cap);
this.elementCount = 0;

this.add = vectorAdd;
this.capacity = new Function("return this.elementData.length");
this.clear = vectorClear;
this.contains = vectorContains;
this.copyInto = vectorCopyInto;
this.elementAt = vectorElementAt;
this.ensureCapacity = vectorEnsureCapacity;
this.firstElement = vectorFirstElement;
this.indexOf = vectorIndexOf;
this.insertElementAt = vectorInsertElementAt;
this.isEmpty = vectorIsEmpty;
this.lastElement = vectorLastElement;
this.lastIndexOf = vectorLastIndexOf;
this.removeElement = vectorRemoveElement;
this.removeElementAt = vectorRemoveElementAt;
this.removeRange = vectorRemoveRange;
this.size = new Function("return this.elementCount");
this.trimToSize = vectorTrimToSize;

}

function vectorAdd(obj)
{
this.elementData[this.elementCount++] = obj;
}

function vectorClear()
{
for(var i = 0;i < this.elementCount;i++)
{
this.elementData[i] = null;
}
}

function vectorContains(obj)
{
if(this.indexOf(obj) >= 0)
return true;
else
return false;
}

function vectorCopyInto(arr)
{
for(var i = this.elementCount;i < arr.length;i++)
{
this.elementData[i] = arr[i];
}
}

function vectorElementAt(index)
{
return this.elementData[index];
}

function vectorEnsureCapacity(cap)
{
if(cap < this.elementData.length)
{
for(var i = 0; i < cap;i++)
{
if(this.elementData[i] == null)
this.elementData[i] = "";
}

}
}

function vectorFirstElement()
{
if(this.elementCount > 0)
return this.elementData[0];
}

function vectorIndexOf(obj)
{
var m;
for(var i = 0;i < this.elementCount;i++)
{
m=true;
for(var p in obj)
{
if(obj[p]!= this.elementData[i][p])
{
m=false;
break;
}
}
if(m)
return i;
}
return -1
}

function vectorInsertElementAt(obj,index)
{
for(var i = this.elementCount;i >= index; i--)
{
this.elementData[i + 1] = this.elementData[i];
}
this.elementData[index] = obj;
this.elementCount++;
}

function vectorIsEmpty()
{
if(this.elementCount == 0)
return true;
else
return false;
}

function vectorLastElement()
{
if(this.elementCount > 0)
return this.elementData[this.elementCount - 1];
}

function vectorLastIndexOf(obj)
{ var m;
for(var i = this.elementCount;i > 0;i--)
{
m = true;
for(var p in obj)
{
if(this.elementData[i][p] != obj[p])
{
m = false;
break;
}
}
if(m)
return i;
}
return -1
}

function vectorRemoveElement(obj)
{
var i = this.indexOf(obj);

this.removeElementAt(i);
}

function vectorRemoveElementAt(index)
{
this.elementData[index] = "";

for(var i = index;i < this.elementCount;i++)
{
this.elementData[i] = this.elementData[i + 1];
}
this.elementCount--;
}

function vectorRemoveRange(start,end)
{
var moveUp = false;
var moveUpTo = start;

for(var i = start;i < end;i ++)
{
this.elementData[i] = null;
}

for(var i = end,moveUpTo;i < this.elementCount;i++,moveUpTo++)
{
this.elementData[moveUpTo] = this.elementData[i];
}

this.elementCount -= (end - start);
}

function vectorTrimToSize(cap)
{
for(var i = cap;i < this.element.length;i++)
{
this.elementData[i] = null;
}
}
alexsten 2003-10-17
  • 打赏
  • 举报
回复
所有的object类型还是得自己在js里面声明,去构造
做到跟vector一样是不可能的
nevana 2003-10-17
  • 打赏
  • 举报
回复
是啊,是啊,再说,没事谁去碰那个可怕的vector啊:)
iinohk 2003-10-17
  • 打赏
  • 举报
回复
要做到跟java中的vector功能一樣強大應該是不可能的…
你看看這個可不可以幫你
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsobjDictionary.asp?frame=true

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧