2010-03-03 240 views
69

我有int數組沒有元素,我試圖檢查它是否爲空。如何檢查數組是否爲空/空?

例如,爲什麼下面的代碼中的if語句從不正確?

int[] k = new int[3]; 

if(k==null) 
{ 
    System.out.println(k.length); 
} 
+0

請問您可以多發一些代碼嗎?數組初始化的位將會很有用。 – 2010-03-03 09:23:12

+0

我不確定你的問題。當然,要檢查一個數組是否爲null,可以這麼說(array == null) – Paul 2010-03-03 09:34:48

+6

你不想要如果(k!= null) – vickirk 2010-03-03 10:51:09

回答

152

null數組和空數組之間有一個關鍵區別。這是對null的測試。

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

「空」在這裏沒有正式的含義。我選擇限定空具有0個元素:

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; 
    } 
} 
+0

ups,最後一個片段有'obj! - null',可能意思是'obj! = null' – 2010-03-03 09:58:25

+2

不要忘記:org.apache.commons.lang3.ArrayUtils.isEmpty(k) – aholub7x 2012-09-21 13:29:28

0

的沒有元素的數組不一定不一定是null。如果尚未分配,它將僅爲null。有關Java陣列的更多信息,請參閱this tutorial

您可以測試數組的長度:

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

我是從.NET的背景。但是,java/c#更多/更少。

如果你實例化一個非基元類型(在你的情況下是數組),它不會爲空。
例如int[] numbers = new int[3];
在這種情況下,空間分配&每個元素都有0

默認值這將是null,當你不new起來。
例如

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

在Java中不會編譯,因爲它會告訴你'numbers'還沒有被初始化。 「未初始化」和「空」不是一回事。 – 2010-03-03 09:30:53

+0

感謝Joachim。我將編輯帖子以將'int []數字'更改爲'int [] numbers == null';在C#中,情況並非如此。 – shahkalpesh 2010-03-03 10:17:44

14

看看它的長度:

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

雖然它的安全,同時檢查null:

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

的int數組與零初始化所以不會實際上包含空值。只有Object的數組最初將包含null。

+0

如果我必須檢查整數爲空 – 2010-03-03 09:30:43

+1

如果使用諸如int之類的原語檢查爲null,那麼該怎麼辦? – objects 2010-03-03 09:35:19

+2

取決於你聲明它的地方,如果作爲一個類成員,那麼是的它被初始化爲零。但是當在方法中本地聲明時,我相信這是另一種情況......你必須自己分配一個初始值。我想。只是一個想法! – ultrajohn 2010-03-03 09:43:35

-1

我相信,你想要的是

int[] k = new int[3]; 

if (k != null) { // Note, != and not == as above 
    System.out.println(k.length); 
} 

你newed它,所以它永遠不會爲空。

29

ArrayUtils.isNotEmpty(testArrayName)org.apache.commons.lang3確保陣列封裝不爲空或空

+0

這實際上是我在找的東西 – 2017-10-09 07:50:50

0

這裏的要點非常簡單地是變量k不爲空,因爲它指向該陣列。數組本身是空的並不重要。如果變量k沒有指向任何內容,那麼在帖子中的空測試只會評估爲真。

1

我測試如下。希望能幫助到你。

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"); 
     } 
0

您還可以檢查是否存在陣列中的任何元素通過找出它的長度,然後把它放進if-else語句來檢查它是否爲null。

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