Java Object Null检查方法 [英] Java Object Null Check for method
本文介绍了Java Object Null检查方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在该公式中为书籍[i]创建一个空检查,并且我不确定如何进行此检查,因为我对空检查并不十分熟悉,并且在编程时还很陌生.任何帮助都将不胜感激!
I need to create a null check in this formula for the books[i] and I am not entirely sure how to go about this as I am not greatly familiar with null checks and very new at programming. Any and all help is much appreciated!
public static double calculateInventoryTotal(Book[] books)
{
double total = 0;
for (int i = 0; i < books.length; i++)
{
total += books[i].getPrice();
}
return total;
}
推荐答案
首先,您应该检查books
本身是否不为null,然后只需检查books[i] != null
:
First you should check if books
itself isn't null, then simply check whether books[i] != null
:
if(books==null) throw new IllegalArgumentException();
for (int i = 0; i < books.length; i++){
if(books[i] != null){
total += books[i].getPrice();
}
}
这篇关于Java Object Null检查方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文