在Java中的for循环中防止null的最佳方法是什么?
这看起来很难看:
if (someList != null) {
for (Object object : someList) {
// do whatever
}
}
Run Code Online (Sandbox Code Playgroud)
要么
if (someList == null) {
return; // Or throw ex
}
for (Object object : someList) {
// do whatever
}
Run Code Online (Sandbox Code Playgroud)
可能没有任何其他方式.他们应该把它放在for构造本身,如果它是null,那么不要运行循环?