# 我怎样才能检查一个数组是否为空/空？

` `int[] k = new int[3]; if(k==null) { System.out.println(k.length); }` `

### 10 Solutions collect form web for “我怎样才能检查一个数组是否为空/空？”

`null`数组和空数组之间有一个关键的区别。 这是一个`null`testing。

` `int arr[] = null; if (arr == null) { System.out.println("array is null"); }` `

` `arr = new int[0]; if (arr.length == 0) { System.out.println("array is empty"); }` `

“空”的替代定义是，如果所有的元素都是`null`

` `Object arr[] = new Object[10]; boolean empty = true; for (int i=0; i<arr.length; i++) { if (arr[i] != null) { empty = false; break; } }` `

` `Object arr[] = new Object[10]; boolean empty = true; for (Object ob : arr) { if (ob != null) { empty = false; break; } }` `

` `int[] i = ...; if (i.length == 0) { } // no elements in the array` `

` `if (i == null || i.length == 0) { }` `

` `int[] numbers = null; // changed as per @Joachim's suggestion. if (numbers == null) { System.out.println("yes, it is null. Please new it up"); }` `

` `Integer[] integers1 = new Integer[10]; System.out.println(integers1.length); //it has length 10 but it is empty. It is not null array for (Integer integer : integers1) { System.out.println(integer); //prints all 0s } //But if I manually add 0 to any index, now even though array has all 0s elements //still it is not empty // integers1[2] = 0; for (Integer integer : integers1) { System.out.println(integer); //Still it prints all 0s but it is not empty //but that manually added 0 is different } //Even we manually add 0, still we need to treat it as null. This is semantic logic. Integer[] integers2 = new Integer[20]; integers2 = null; //array is nullified // integers2[3] = null; //If I had int[] -- because it is priitive -- then I can't write this line. if (integers2 == null) { System.out.println("null Array"); }` `

` `void foo(int[] data) { if(data.length == 0) return; }` `

` `int[] k = new int[3]; if(k.length == 0) { //do something }` `

` `int[] k = new int[3]; if (k != null) { // Note, != and not == as above System.out.println(k.length); }` `

• 在PHP中移植multidimensional array
• 无法在PHP中连接2个数组
• 如何在Python中声明和添加项目到一个数组？
• 我如何find二维数组的大小？
• 如何将新元素添加到数组？
• 如何将具有已知键的数组元素移动到PHP中数组的结尾？
• 如何在jQuery中循环数组？
• 字节数组到图像对象
• 为什么Lua数组（表）从1开始而不是0？
• 如何将数值和键都推到数组中
• 将JSONstring转换为Javascript中的JSON对象数组