长度为0的数组和 null
来源:互联网 发布:spark mongodb python 编辑:程序博客网 时间:2024/05/17 22:30
长度为0的数组 int[] arr = new int[0],也称为空数组,虽然arr长度为0,但是依然是一个对象
null数组,int[] arr = null;arr是一个数组类型的空引用。
1. 编写api方法,进行参数校验时,不要漏掉空数组的情况
比如下面这个计算递增子序列最大长度的方法,要考虑空数组的情况。
public class Solution { public int lengthOfLIS(int[] nums) { if (nums == null || <span style="color:#ff0000;">nums.length == 0</span>) { return 0; } int size = nums.length; int[] itemLengthArray = new int[size]; int currentMax = 0; int outMax = 1; for (int k = 0 ; k < size; ++k) { itemLengthArray[k] = 1; } for (int i = 1; i < size; ++i) { for (int j = 0; j < i; ++j) { if (nums[j] < nums[i]) { if (currentMax < itemLengthArray[j]) { currentMax = itemLengthArray[j]; } } } itemLengthArray[i] = currentMax + 1; currentMax = 0; outMax = outMax > itemLengthArray[i] ? outMax : itemLengthArray[i]; } return outMax; }}
2. Effective Java第43条(返回零长度的数组或者集合,而不是null)清楚的说明了零长度或者集合的好处,可以避免调用api的客户端进行不必要的非null判断
public String[] getIpList() { if (ipList.size != 0) { ...... } return null;}
由于该方法可能返回空,客户端调用上述方法没次都需要进行非null判断。
0 0
- 长度为0的数组和 null
- 数组长度为0和数组为null的区别
- JAVA 长度为0的数组和 null
- 数组长度为0和数组为null
- java数组长度为0和数组为null
- 数组长度为0与NULL
- 数组长度为零和空引用的区别 files!=null&files.length()>0 空指针或赋空值
- Java中长度为0的数组与null的区别
- Java中长度为0的数组与null的区别
- Java中长度为0的数组与null的区别
- 长度为0的数组
- 长度为0的数组
- java中数组为空和数组长度为0的区别
- 长度为0数组
- 长度为0的动态数组
- C语言长度为0的数组
- php 长度为0的数组
- 浅析长度为0的数组
- Unsupported major.minor version (jdk版本错误)解决方案 办法
- Android编译系统入门(一)
- Java中Map的遍历
- 关于MongoDB的group用法
- iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解
- 长度为0的数组和 null
- Codeforce 414B(递推)
- 【2015-2016 ACM-ICPC Pacific Northwest Regional Contest (Div 1)D】【水题】Triangle 两个三角形是否可以恰好拼成矩形
- LT8911 市面上兼容性最好的EDP桥接IC,LVDS转EDP,mipi转EDP
- Callback Hell-Javascript异步编程指导
- #学习笔记#(4)输入框提示信息不能为空--JavaScript改变CSS样式
- Xcode 和Interface Builder
- 为什么Maven会更改Eclipse JDK设置
- leetcode -- Rectangle Area -- 看看计算公式