以下代码的类似代码是什么?

Hay*_*yan 2 java syntax

我想了解以下代码:

this.area[y][x + i] = tmp != null ? String.valueOf(tmp.charAt(i)) : " ";
Run Code Online (Sandbox Code Playgroud)

这是类似的代码吗?

if(tmp != null){
   this.area[y][x + i] = String.valueOf(tmp.charAt(i));
} else {
   this.area[y][x + i] = "";
}
Run Code Online (Sandbox Code Playgroud)

Abh*_*hek 5

不,他们不一样!你在三元运算符中有空间,但没有if-else.相同/相似的代码将是,

if(tmp != null){
this.area[y][x + i] = String.valueOf(tmp.charAt(i));
} else {
this.area[y][x + i] = " "; //observe white-space, maybe important for your case
}
Run Code Online (Sandbox Code Playgroud)