簡體   English   中英

如何檢查數組是否為空或數組內容是否為空

[英]How to check if array is null or if the array contents are null

什么是檢查數組是最好的方式null值,或者如果數組內容是null的組合在Java 6中1個語句:

if ((myArray[0] != null) || (myArray[1] != null) || (myArray!= null)) {
    ...
}

首先,檢查數組本身是否不為null 如果數組為null ,則沒有理由對其元素進行迭代,因為Java在訪問它時將拋出NullPointerException

if (myArray != null) {
    // ...
}

然后在條件主體內部遍歷其所有元素,並檢查其中之一是否為null

boolean hasNull = false;
for (int i=0; i<myArray.length; i++) {
    if (myArray[i] == null) {
        hasNull = true;
        break; // to terminate the iteration since there is no need to iterate more
    } 
}

這種單行解決方案(感謝@Napstablook的警告)。 條件被評估為true ,如果數組本身是null或它的元件之一是null

if !(myArray != null && myArray[0] != null && myArray[1] != null) { ... }

請注意, &&運算符的作用是,如果左側被評估為false ,則它將停止評估其余條件,因為它不會影響結果。 || 但是true 但是,我建議您避免這種解決方案,因為索引可能會溢出。 最好使用上面提到的for-loop

為了讓它檢查任何值,我將使用allMatch 首先檢查數組!= null也很重要,否則,將得到Exception。

if (array == null || Arrays.stream(array).allMatch(Objects::isNull)) 

請注意,這不適用於版本8之前的Java,在我發布答案后,OP編輯了他的要求

檢查數組是否為空:

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

檢查數組是否為空:

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

同時檢查null:

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

if (array == null || iarray.length == 0) { }

嘗試這個

 if (myArray == null || Arrays.stream(myArray).allMatch(element-> element==null)) {}

編輯-對於Java 6,我真的看不到這一行。 如果不需要一行,可以嘗試一下

  boolean isNull = true;
    if(myArray==null){
        System.out.println("array is null");
    }else{
        for(Integer element: myArray){
            if(element!=null){
                System.out.println("array is not null");
                isNull=false;
                break;
            }
        }
        if(isNull)
            System.out.println("Array is null");
    }

例如這個

boolean isNullOrContainsNull = array == null || Arrays.asList(array).contains(null);

在一行中檢查數組是否為null或包含null元素

如果要檢查數組為空還是空或所有元素都為空,請執行

boolean containsNothingUseful = array == null
        || array.length == 0
        || !new HashSet<String>(Arrays.asList(array))
            .retainAll(Arrays.asList((String)null));

(假設一個String[]數組)

使用Collection#retainAll()方法,該方法在存在其他值(即與“ containsOnly”相反Collection#retainAll()時返回true

使用這種襯里實​​際上實際上是效率低下的,更好的方法是使用如下所示的方法,該方法不會創建大量的臨時對象和變異集合等。

public static boolean containsNothingUseful(String[] array) {
    if (array == null || array.length == 0)
        return true;
    for (String element : array) {
        if (element != null)
            return false;
    }
    return true;
}

// ...
if (containsNothingUseful(myArray)) { .. }

代替Itreating Manullay數組,在這種情況下重新使用現有的集合。

  1. 將數組轉換為列表
  2. 檢查null是否存在於列表中或不使用contains()方法;

請找到示例代碼:

public static void main(String[] args) {
        Integer[] array = new Integer[3];

        array[0] = 1;
        array[1] = null;
        array[2] = 2;

        System.out.println(Arrays.asList(array).contains(null));
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM