在循环之前检查列表是否为空的最佳方法是什么?

发布于 2024-10-26 23:09:00 字数 493 浏览 1 评论 0原文

我知道这是一个原始问题,但我想学习最聪明的方法。

我想循环 ArrayList; intList 并且它可以为空。我有两种方法可以做到这一点

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清晨说晚安 2024-11-02 23:09:00

在这种情况下,我也会选择第一个实现,因为它的意图更清晰。

一般来说,我会尽量避免 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 other Collection object, really) being null. When evaluating a List (which is suddenly and unexpectedly null) you most probably want to abort before any processing takes place so either case of looping over the collection would not occur.

泪冰清 2024-11-02 23:09:00

我们公司有一条开发规则:

  • 如果编写的函数返回列表或数组,永远不要返回 null!如果没有要返回的元素,则返回空列表或空数组。

这可以用最小的开销来完成,如下所示:

public List<String> getNames() {
    if( !loaded ) {
        return Collections.emptyList();
    }
    ...
}

如果应用正确,您不必检查空列表。我们不必这样做。

编辑:哦,回到你手头的问题:使用 forst 变体,它更清晰,更快,因为空检查只需要完成一次(可能是编译器无论如何都会将其分解出来,但在变体 1 中你可以确定)。

We have one development rule in our company:

  • If a function is written that returns a list or an array, never return null! Return an empty list or an empty Array in the case where there are no elements to return.

This can be done with minimal overhead, like here:

public List<String> getNames() {
    if( !loaded ) {
        return Collections.emptyList();
    }
    ...
}

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).

一场信仰旅途 2024-11-02 23:09:00

我更喜欢第一个,因为完整的代码段很可能是:

if(intList != null) {
 for(int i = 0; i < intList.size(); i++){
         System.out.println(intList.get(i));
 }
}
else {
   //Do something here.
}

I prefer the first one, for the complete code segment will most likely to be:

if(intList != null) {
 for(int i = 0; i < intList.size(); i++){
         System.out.println(intList.get(i));
 }
}
else {
   //Do something here.
}
心碎无痕… 2024-11-02 23:09:00

我更喜欢第一个,因为我主要使用第一个过程

i preffer the first one as mostly i use the first process

驱逐舰岛风号 2024-11-02 23:09:00

对我来说,第一个选项更清晰、更容易阅读和理解。

To me the first option is clearer and easier to read and understand.

一身软味 2024-11-02 23:09:00

根据我的说法,第一个选项应该是首选,因为它的可读性比第二个更好,而第二个选项可以为您节省一行额外的代码。

一天结束时,两者都会做同样的事情,所以这取决于你,你想使用哪个代码。我建议坚持第一个,因为它具有可读性,对开发人员更友好。

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.

束缚m 2024-11-02 23:09:00

第一种形式更具可读性——意图更清晰。

第一种形式可能也更快,因为第二种形式要求每次循环时测试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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文