说明:为了避免在数组和集合的获取上增加null的判断,同时也能减少不必要的空指针异常,通常会在业务返回零数组或空集合. 方法: 1.数组: 定义全局静态常量来减少内存开销:private static final int EMPTY_ARRAY = new int[0];,当然,也可以直接局部变量等等. 2.集合: 使用工具类Collections的emptyList()进行放回. 参考: http://blog.csdn.net/tounaobun/article/details/850216…
返回零长度的数组或集合,而不是null 像下面的方法并不少见: private final List<Cheese> cheesesInStock = ...; /** * @return an array containing all of the cheeses in the shop, * or null if no cheese are available for purchase. */ public Cheese[] getCheeses(){ if(cheesesInStoc…