无法读取未定义的2d数组的属性“0”

xzlaal3s  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(168)

**已关闭。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想要改进此问题?**更新问题,使其位于堆栈溢出主题上。

昨天关门了。
改进这个问题
我试图访问2d数组中的元素,控制台正在输出 Cannot read property '0' of undefined .
作为参数传递的数组:

a = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
Getting element 1: a[0][0] -> outputs 1 works as expected
Getting element 2: a[0][1] -> outputs 2 works as expected
Getting element 3: a[0][2] -> outputs 3 works as expected

Getting element 4: a[1][0] -> Cannot read property '0' of undefined
Getting element 5: a[1][1] -> Cannot read property '1' of undefined
Getting element 6: a[1][2] -> Cannot read property '2' of undefined

Getting element 7: a[2][0] -> same error as above, but a[a.length-1][0] works
Getting element 8: a[2][1] -> same error as above, but a[a.length-1][1] works
Getting element 9: a[2][2] -> same error as above, but a[a.length-1][2] works

For 4,5,6 I cant do a[a.length-2][0 1 or 2] as it still gives the same error

如何访问第二个数组及其值?
还有,为什么会这样 a[2][2] 不工作,但是 a[a.length-1][2]
如果 a[a.length-1][0] 行得通,为什么不行 a[a.length-2][0] 工作

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题