【问题标题】:find if the array element is null or not in 2D array在二维数组中查找数组元素是否为空
【发布时间】:2018-06-26 10:28:38
【问题描述】:

我正在编写这个特定的程序,我收到如下错误:-

表达式的类型必须是数组类型,但它解析为 诠释。

错误在这一行:- if (a[i][j] != null) 我想检查数组元素是否为null

    Integer [][] arr = new Integer [6][6];
    int a,b;
    a=0;b=0;
    for (int i=0;i<4;i++)
    {          
        for (int j=0;j<4;j++)
        {
            if (a[i][j] != null)
            {
                a=a+a[i][j];
            }

            if(b<a)
        {
            b=a;
        }
        }

    }

【问题讨论】:

  • 你确定你写过a[i][j],还是写错了问题
  • 查看代码的前几行。 a 的类型是什么?可以索引吗?顺便说一句,当您使用无意义的变量名(如 ab 等)时会发生这种情况。
  • a[i][j]arr[i][j] ?
  • 您的代码有误。 arr[i][j] 而不是 a[i][j]

标签: java arrays null


【解决方案1】:

问题是您将 a 定义为 int 变量 int a,b; 并将其用作 array a[i][j]
因此,请确保将 if (a[i][j] != null) 更改为 if (arr[i][j] != null)
并将a=a+a[i][j]; 更改为a=a+arr[i][j];a+=arr[i][j];

【讨论】:

    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多