[英]How to use empty list when we have an empty list anyways
This question is a bit hard to explain so please look at the increasingPath
function in the code here . 这个问题有点难以解释,因此请在此处查看代码中的
increasingPath
功能。 Now assuming the path does not
exist, and arraylist pointPath
with 0 size is returned. 现在假设该路径
does not
存在,并且返回大小为0的arraylist pointPath
。 However I have read that we must use Collections.emptyList, but how do I use it in this scenario, when I already have a probably empty list called pointPath
? 但是我已经读到我们必须使用Collections.emptyList,但是当我已经有一个可能为空的名为
pointPath
列表时,如何在这种情况下使用它? If not to use Collections.emptyList
then when to use it ? 如果不使用
Collections.emptyList
那么何时使用它?
I think what you're supposed to do is use Collections.EmptyList for comparisons and returning. 我认为您应该做的是使用Collections.EmptyList进行比较和返回。
for example: 例如:
if (pointPath.equals(Collections.emptyList()){
return Collections.emptyList();
}
I don't think it changes how your program will execute, but it makes the code readable and self-documenting. 我不认为它会改变程序的执行方式,但可以使代码易于阅读和自我记录。
Simply check 只需检查
if (pointPath.isEmpty()){
return Collections.emptyList();
}
The only difference from actually returning a your list that is accidentally empty is that Collections.emptyList()
is immutable list. 与实际返回意外为空的列表的唯一区别是
Collections.emptyList()
是不可变的列表。 If that is not of any value to you, I'd return your real list. 如果那对您没有任何价值,我将退还您的真实名单。 That way there is less code to read.
这样,更少的代码可读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.