List<Foo> results = null;
results = this.getResults();
if (results == null || results.size() == 0)
{
LOGGER.warn("results empty");
}
else
{
LOGGER.warn("results:" + results.toString());
}
Run Code Online (Sandbox Code Playgroud)
getResults返回空List 时,上面的代码总是产生以下输出.
results:[null]
Run Code Online (Sandbox Code Playgroud)
我需要响应这个空方案,但不知道如何捕获它.
我认为结果列表有一个项为空值.
我认为方法是检查列表中是否包含空值0,如果不是,则列表包含至少一个非空值.
List<Foo> results = null;
results = this.getResults();
if (results == null || results.size() == 0) {
LOGGER.warn("results empty");
} else if (list.get(0) == null){
LOGGER.warn("the list has only an null value");
} else {
LOGGER.warn("results:" + results.toString());
}
Run Code Online (Sandbox Code Playgroud)
要么
List<Foo> results = null;
results = this.getResults();
if (results == null || results.size() == 0 || list.get(0) == null) {
LOGGER.warn("results empty");
} else {
LOGGER.warn("results:" + results.toString());
}
Run Code Online (Sandbox Code Playgroud)