java 为什么增强的 for 循环不执行空检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3984626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:15:48  来源:igfitidea点击:

Why enhanced for loop is not performing the null checking

java

提问by Sujith

Why the enhanced for loop is not performing null checking before iterating over the collection.

为什么增强的 for 循环在迭代集合之前不执行空检查。

回答by Jon Skeet

If you mean that this would go bang:

如果你的意思是这会爆炸:

int[] array = null;
for (int x : array) {
}

... then I think it's entirely proper for that to throw a NullPointerException. The alternative would be for it to silently swallow the null, and treat that as equivalent to an empty array. That's not the approach Java takes anywhere else - why should this be different? It would make the language inconsistent.

...然后我认为抛出一个NullPointerException. 另一种方法是让它默默地吞下空值,并将其视为等效于一个空数组。这不是 Java 在其他任何地方采用的方法 - 为什么会有所不同?这会使语言不一致。

The one place I wish there weremore handling (but explicit handling) is switching on an enum - it would be nice to be able to provide a case for null, to avoid checking for that one special value beforehand. But that's a very different construct, which is specifically trying to take different actions for different values.

一个地方,我希望有更多的处理(但明确处理)上的枚举切换-这将是很好能够为空提供的情况下,可以不必提前检查一个特殊的价值。但这是一个非常不同的构造,它专门尝试针对不同的值采取不同的操作。

回答by Francisco López-Sancho

I kind of afraid to assert against Jon Skeet... :| ... But what a hell... what do you think about this scenario?:

我有点害怕对 Jon Skeet 断言... :| ......但是到底是什么......你如何看待这种情况?:

`

`

findMoney(List<Places> places){
   int money = 0;
   for(Place place : places){
   money += searchWithCare(place);
   }
return money;
}

`

`

If I ask some client to tell me where to look and he tells me nothing. Do you think is better here to throw a NullPointerException? May be manage the call to this method so if parameter is null not call it?.... I think that return 0 is a consistent response because no error is here. I believe Exceptions are made to manage flow of code and I dont see why should I change the flow in this kind of situation. Return a cero or even a null could be a good response, couldnt it be?

如果我问一些客户告诉我去哪里看,他什么也没告诉我。你认为这里抛出 NullPointerException 更好吗?可能是管理对这个方法的调用,所以如果参数为空不调用它?..我认为返回 0 是一致的响应,因为这里没有错误。我相信 Exceptions 是用来管理代码流的,我不明白为什么我应该在这种情况下改变代码流。返回 cero 甚至 null 可能是一个很好的响应,不是吗?

So what about this solution? `

那么这个解决方案呢?`

findMoney(List<Places> places){
   int money = 0;
   for(Place place : places == null ?Collections.<Place>emptyList():places){
   money += searchWithCare(place);
   }
return money;
}

`

`

回答by shijin

The code must be like this:

代码必须是这样的:

for (AnyObject anyObject : checkIsEmpty(anyObjectList)) {
        System.out.println(anyObject.doSomething());
}

private <T> Iterable<T> checkIsEmpty(Iterable<T> iterable) {
        return iterable == null ? Collections.<T>emptyList() : iterable;
}