Arrays For循环无法识别2d数组的名称

Arrays For循环无法识别2d数组的名称,arrays,loops,for-loop,Arrays,Loops,For Loop,我有一个2d数组,我的目标是打印2d数组的总长度。但是,我得到了一个错误与此代码。在for循环头中,编译器不会识别名称“arrays”,并建议我使用该名称创建一个局部变量。 我已经尝试在for循环之前计算1d array.length,但这也不起作用,因为编译器仍然无法识别名称“arrays” int[][]数组={{{2,5,3,8,5}, {3, 5, 1, 7, 2}, { 6, 2, 8, 1}}; } 整数长度=0; for(int i=0;i

我有一个2d数组,我的目标是打印2d数组的总长度。但是,我得到了一个错误与此代码。在for循环头中,编译器不会识别名称“arrays”,并建议我使用该名称创建一个局部变量。

我已经尝试在for循环之前计算1d array.length,但这也不起作用,因为编译器仍然无法识别名称“arrays”

int[][]数组={{{2,5,3,8,5},
{3, 5, 1, 7, 2},
{ 6, 2, 8, 1}};
}
整数长度=0;
for(int i=0;i
你好,LoFitz我看到的问题是您将数组的名称放在括号之前。而不是:

int[][] arrays
使用

希望这有帮助。

数组[I][j]。长度

使用它可能有助于u bruh

int[][]数组={{2,5,3,8,5},
    int[][] arrays = { {2, 5, 3, 8, 5},
            {3, 5, 1, 7, 2},
            { 6, 2, 8, 1}};

int Length = 0;
for (int i= 0; i< arrays.length; i++) {
    Length += arrays[i].length;
    System.out.println(+Length);
}
{3, 5, 1, 7, 2}, { 6, 2, 8, 1}}; 整数长度=0; for(int i=0;i
此处,您在
int Length=0之前添加了额外的括号这就是它给出错误的原因,因为函数的作用域在使用该数组之前被关闭。所以请随时检查这个question@AnkitPrajapati是正确的,由于一个额外的括号,它的代码块在int Length=0之前被关闭;编译器假设较低的代码位于不同的范围请提供适当的解决方案和适当的注释。尝试删除int length之前的花括号添加一些解释/详细信息以及代码。这里我删除了int length=0语句之前的花括号,以便for循环中的array.length内容帮助我们使用数组的范围已定义为hanks。单击“编辑”按钮并将其添加到您的答案中,以提高可读性。
int arrays [][] 
    int[][] arrays = { {2, 5, 3, 8, 5},
            {3, 5, 1, 7, 2},
            { 6, 2, 8, 1}};

int Length = 0;
for (int i= 0; i< arrays.length; i++) {
    Length += arrays[i].length;
    System.out.println(+Length);
}