JAVA数组中length的问题?

enjoyxiao 2007-06-18 01:04:13
下面的程序
public class Tables {
public static void main(String[]args) {

final int TABLE_SIZE = 5;
int[][] table = new int[TABLE_SIZE][TABLE_SIZE];
int[] table2 = new int[TABLE_SIZE];

int a =table.length;
int b =table2.length;

System.out.println(a);
System.out.println(b);
}
}

都是输出 5

如上面程序里如何得到table的大小?arrays.length()到底返回什么属性?
...全文
1773 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
enjoyxiao 2007-06-18
  • 打赏
  • 举报
回复
shuhang1106():
java的多维数组,并不是像一个二维表一样,而是数组的数组,也就是说建立一个数组a,在数组a中存放的每个元素又是一个数组,所以在计算a.length的时候计算的是数组a中存放了多少个数组的引用,在你的程序中table.length和table2.length的值都为5。

应该是这样吧.
weihthchk 2007-06-18
  • 打赏
  • 举报
回复
二维数组相当于由一维数组为元素构成的一维数组,作为元素的数组长度可以是不固定的……
shuhang1106 2007-06-18
  • 打赏
  • 举报
回复
java的多维数组,并不是像一个二维表一样,而是数组的数组,也就是说建立一个数组a,在数组a中存放的每个元素又是一个数组,所以在计算a.length的时候计算的是数组a中存放了多少个数组的引用,在你的程序中table.length和table2.length的值都为5。
tomkai 2007-06-18
  • 打赏
  • 举报
回复
这个貌似是一个数学问题..

数组的长度就是其第一阶的值决定...(理论的语言说不上来)
justinavril 2007-06-18
  • 打赏
  • 举报
回复
如果是tabel.length就返回table[size1][size2] size1
如果是tabel[0].length,tabel[1].length,tabel[0].length...就返回size2的
这样吧
fengmingjie 2007-06-18
  • 打赏
  • 举报
回复
好像显示的每一层的维数,如果要求数组的长度,可以直接维度相乘
fengmingjie 2007-06-18
  • 打赏
  • 举报
回复
public static void main(String[] args) {

final int TABLE_SIZE = 5;
final int TABLE_SIZE1 = 6 ;
int[][] table = new int[TABLE_SIZE1][TABLE_SIZE];
int[] table2 = new int[TABLE_SIZE];
/*for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
table[i][j]=0;
}
}*/
int a =table[1].length;
int b =table2.length;
int c = table.length;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
输出的结果是5,5,6,好像都是每个数组的外层结构的引用的个数
tdy1234 2007-06-18
  • 打赏
  • 举报
回复
你改成getLength看看
xiaxinhuo 2007-06-18
  • 打赏
  • 举报
回复
学习了
justinavril 2007-06-18
  • 打赏
  • 举报
回复
public class temp1 {
public static void main(String[]args) {

final int TABLE_SIZE = 5;
final int TABLE_SIZE1 = 6 ;
int[][] table = new int[TABLE_SIZE][TABLE_SIZE1];
int[] table2 = new int[TABLE_SIZE];
int a =table[0].length;
int b =table2.length;

System.out.println(a);
System.out.println(b);
}
}
这个程序能说明你的问题 希望吧
xiaoniaoxiangfei 2007-06-18
  • 打赏
  • 举报
回复
那就不应该怪了,二唯的就是一唯的转换过来的。
aptweasel 2007-06-18
  • 打赏
  • 举报
回复
路过。我顶一下先。
enjoyxiao 2007-06-18
  • 打赏
  • 举报
回复
我知道是6和5,就是觉得很奇怪.
xiaoniaoxiangfei 2007-06-18
  • 打赏
  • 举报
回复

public class temp {
public static void main(String[]args) {

final int TABLE_SIZE = 5;
final int TABLE_SIZE1 = 6 ;
int[][] table = new int[TABLE_SIZE1][TABLE_SIZE];
int[] table2 = new int[TABLE_SIZE];
int a =table.length;
int b =table2.length;

System.out.println(a);
System.out.println(b);
}
}


========================
是一是这个输出是什么你就知道到底是怎么一回事了。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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