二维数组的长度

shnewtouch 2004-10-22 08:51:16
var company=new Array(new Array(),new Array());
company是一个二维数组,我想知道他的长度,比如:company[0]的长度。怎么实现呢?谢谢了!
...全文
1380 9 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
shnewtouch 2004-10-22
  • 打赏
  • 举报
回复
<script language="javascript">
var company=new Array(new Array(),new Array());
company[0][0]="a";
company[0][1]="b";
company[0][2]="c";
company[1][0]="d";
company[2][0]="e";
company[3][0]="f";
var i=0;
for(i=0;i<company[i].length;i++)
document.write(company[i][0]);
</script>
为什么有错误啊?谢谢
shnewtouch 2004-10-22
  • 打赏
  • 举报
回复
楼上的是什么意思啊?能说的详细点吗?谢谢!
禽兽v5 2004-10-22
  • 打赏
  • 举报
回复
var company=new Array(new Array(),new Array());

一开始new Array(),空的,然后用
company[0][0] = "a1";
company[0][1] = "a2";
来添加数据
那么你再用company[0].length想得到改变后的长度,是得不到的。

往Array对象添加/删除元素:
<SCRIPT LANGUAGE="JavaScript1.2">

myFish = ["angel", "clown", "mandarin", "surgeon"];
document.writeln("myFish: " + myFish + "<BR>");

removed = myFish.splice(2, 0, "drum");
document.writeln("After adding 1: " + myFish);
document.writeln("removed is: " + removed + "<BR>");

removed = myFish.splice(3, 1)
document.writeln("After removing 1: " + myFish);
document.writeln("removed is: " + removed + "<BR>");

removed = myFish.splice(2, 1, "trumpet")
document.writeln("After replacing 1: " + myFish);
document.writeln("removed is: " + removed + "<BR>");

removed = myFish.splice(0, 2, "parrot", "anemone", "blue")
document.writeln("After replacing 2: " + myFish);
document.writeln("removed is: " + removed);

</SCRIPT>

This script displays:

myFish: ["angel", "clown", "mandarin", "surgeon"]

After adding 1: ["angel", "clown", "drum", "mandarin", "surgeon"]
removed is: undefined

After removing 1: ["angel", "clown", "drum", "surgeon"]
removed is: mandarin

After replacing 1: ["angel", "clown", "trumpet", "surgeon"]
removed is: drum

After replacing 2: ["parrot", "anemone", "blue", "trumpet", "surgeon"]
removed is: ["angel", "clown"]
stefli 2004-10-22
  • 打赏
  • 举报
回复
company[0].length
是正确的哦~
仔细检查~
fason 2004-10-22
  • 打赏
  • 举报
回复
对的,如果访问不到是你程序其它地方错了
shnewtouch 2004-10-22
  • 打赏
  • 举报
回复
不对的把,这样的写法我用过了,可是不行啊,,还有什么其他的办法吗
fason 2004-10-22
  • 打赏
  • 举报
回复
JavaScript没有严格意义上的多维数组。

你要的答案是 company[0].length
micker 2004-10-22
  • 打赏
  • 举报
回复
var company=new Array(new Array(),new Array());
行数:company.length;
company[0].length;
列数,不一定.因为js里和java一样,是不等长的数组,即可以出现这种数组{{"a","b","c","d"},{"e"},{"f","g","f"}};所以各行的列数不一定.

其实java中多维数组是这样处理的:js只有一个一维数组,而此一维数组的元素又可以是一个一维数组,依次类推....
禽兽v5 2004-10-22
  • 打赏
  • 举报
回复
发现IE6好像变强大了......


var company = new Array();

company[0] = new Array();
company[0][0] = "a";
company[0][1] = "b";
company[0][2] = "c";

company[1] = new Array();
company[1][0] = "d";

company[2] = new Array();
company[2][0] = "e";

company[3] = new Array();
company[3][0] = "f";

for( i = 0; i < company.length; i++ )
{
for ( j = 0; j < company[i].length; j++ )
{
document.writeln("company[" + i + "][" + j + "] = " + company[i][j] + "<br>");
}
}
相关推荐
发帖
JavaScript

8.7w+

社区成员

Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
帖子事件
创建了帖子
2004-10-22 08:51
社区公告
暂无公告