我试图插入到单元格数组中,但是仅当该单元格为空时。
就我而言,这意味着null因为我初始化了一个空的char数组,并且只想在单元格为空(null)时才插入。
测试:if grid[i][j] == null不起作用,我得到了"== cannot be applied to 'char', null"。
正确的方法是什么?
在我的情况下,这意味着 null 因为我初始化了一个空的 char 数组
不正确。a 的默认值char是'\0',例如new char[3][4]创建 3 个子数组的外部数组,每个子数组包含 4 个'\0'值。
所以,你的if陈述应该是:
if (grid[i][j] == '\0')
Run Code Online (Sandbox Code Playgroud)