在循环之前检查列表是否为空的最佳方法是什么?
我知道这是一个原始问题,但我想学习最聪明的方法。
我想循环 ArrayList
if(intList != null) {
for(int i = 0; i < intList.size(); i++){
System.out.println(intList.get(i));
}
}
,
for (int i = 0; intList != null && i < intList.size(); i++){
System.out.println(intList.get(i));
}
第一种方法对我来说似乎更漂亮。你怎么认为?在这种情况下你的实施是什么?
对不起,如果是重复的问题,但我找不到,
谢谢
I know it is a primitive question but I want to learn the smartest way.
I want to loop over the ArrayList<Integer> intList
and it can be null. I have 2 ways of doing it
if(intList != null) {
for(int i = 0; i < intList.size(); i++){
System.out.println(intList.get(i));
}
}
and
for (int i = 0; intList != null && i < intList.size(); i++){
System.out.println(intList.get(i));
}
First way seems more pretty to me. What do you think? What are your implementations in that situation?
Excuse me, if it is duplicate question but I can't find one
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在这种情况下,我也会选择第一个实现,因为它的意图更清晰。
一般来说,我会尽量避免
List
(或者任何其他Collection
对象,实际上)是null
。当评估List
(突然且意外地null
)时,您很可能希望在任何处理发生之前中止,这样就不会发生循环集合的任何一种情况。In this case I would choose the first implementation as well because its intent is clearer.
Generally, I would try to avoid a
List
(or any otherCollection
object, really) beingnull
. When evaluating aList
(which is suddenly and unexpectedlynull
) you most probably want to abort before any processing takes place so either case of looping over the collection would not occur.我们公司有一条开发规则:
这可以用最小的开销来完成,如下所示:
如果应用正确,您不必检查空列表。我们不必这样做。
编辑:哦,回到你手头的问题:使用 forst 变体,它更清晰,更快,因为空检查只需要完成一次(可能是编译器无论如何都会将其分解出来,但在变体 1 中你可以确定)。
We have one development rule in our company:
This can be done with minimal overhead, like here:
If applied properly, you don't have to check for null-lists. We don't have to.
EDIT: Oh, and to come back to your question at hand: Use the forst variant, it is much clearer, and faster, because the null check only have to be done once (it might be that the compiler factors it out anyway, but in variant 1 you can be sure).
我更喜欢第一个,因为完整的代码段很可能是:
I prefer the first one, for the complete code segment will most likely to be:
我更喜欢第一个,因为我主要使用第一个过程
i preffer the first one as mostly i use the first process
对我来说,第一个选项更清晰、更容易阅读和理解。
To me the first option is clearer and easier to read and understand.
根据我的说法,第一个选项应该是首选,因为它的可读性比第二个更好,而第二个选项可以为您节省一行额外的代码。
一天结束时,两者都会做同样的事情,所以这取决于你,你想使用哪个代码。我建议坚持第一个,因为它具有可读性,对开发人员更友好。
According to me first option should be preferred for its readability is better than second one, while the second one saves one extra line of code for you.
End of the day both are going to do same thing so its up to you, which code you want to use. I would suggest to stick to first as it is more developer friendly because of readability.
第一种形式更具可读性——意图更清晰。
第一种形式可能也更快,因为第二种形式要求每次循环时测试
intList
不为空。 (JIT 编译器可能优化掉不必要的测试......但为什么要依赖这个。)The first form is more readable - the intent is much clearer.
The first form may also be faster, since the second form says to test that
intList
is not null each time you go around the loop. (The JIT compiler may optimize away the unnecessary tests ... but why rely on this.)