简体   繁体   English

如果要检查数组是否为null或为空的语句,则不起作用

[英]If statement to check if array is null or empty, doesn't work

I got my main task to work (rough translation): 我的主要任务是工作(粗略翻译):

Write a method, which returns the smallest number from a 1d array of integers. 编写一个方法,从1d的整数数组中返回最小的数字。

Now i have to do a side task (rough translation): 现在我必须做一个侧面任务(粗略翻译):

Add an if statement to check if the array is null or empty, and if it is, return 0. 添加if语句以检查数组是空还是空,如果是,则返回0。

method: 方法:

public static int smallestNumber (int[] array1) {

    int smallest = array1[0];

    if (array1 == null || array1.length == -1) {
       return 0;
    }

    else {
       for (int element : array1) {
          if (element < smallest) {
           smallest = element;
          }
       }
    }
return smallest;    
}

main: 主要:

public static void main(String[] args) {
  int[] tab1 = {}; //empty array?
  int smallestNumber = smallestNumber(tab1);
  System.out.printf("The smallest number is: %d", smallestNumber);
}

The method works if i only check for null. 如果我只检查null,则该方法有效。 But i'm confused why it wont work on empty array. 但我很困惑,为什么它不会在空阵列上工作。

int[] tab1 = {};

EDIT: I also tried with array1.length == 0; 编辑:我也尝试过array1.length == 0;

Firstly, Arrays are of non-negative size so array1.length cannot be -1, instead make the comparison with 0 . 首先,数组的大小为非负数,因此array1.length不能为-1,而是与0进行比较。

Secondly, the assignment int smallest = array1[0]; 其次,赋值int smallest = array1[0]; tries to access 0th position of an empty array which will result in java.lang.ArrayIndexOutOfBoundsException . 尝试访问一个空数组的第0个位置,这将导致java.lang.ArrayIndexOutOfBoundsException

So in conclusion move the assignment to smallest in the else block and check the condition for empty or null array before you try to access any array value. 因此,最后在else块中将赋值移动到smallest ,并在尝试访问任何数组值之前检查空数组或空数组的条件。

public static int smallestNumber (int[] array1) {

    int smallest;

    if (array1 == null || array1.length == 0) {
        return 0;
    }

    else {
        smallest = array1[0];
        for (int element : array1) {
            if (element < smallest) {
                smallest = element;
            }
        }
    }
    return smallest;
}
public static int smallestNumber (int[] array1) {

    if (array1 == null || array1.length == 0) {
       return 0;
    }

    int smallest = array1[0];

       for (int element : array1) {
          if (element < smallest) {
           smallest = element;
          }
       }

    return smallest;    
}

Try checking for null first, as it is safer to check for that first. 首先尝试检查null,因为首先检查是否更安全。 You also can use the method isEmpty() for the length. 您还可以使用方法isEmpty()作为长度。

public static int smallestNumber (int[] array1) {

if (array1 == null) {
    return 0;
}

if (array1.isEmpty()) {
    return 0;
}

   int smallest = array1[0];
   for (int element : array1) {
      if (element < smallest) {
           smallest = element;
      }
   }
    return smallest;
}

or add this as an alt: 或者将其添加为alt:

public static int smallestNumber (int[] array1) {

if (array1 != null) {
    if (array1.isEmpty()) {
        return 0;
    }

  int smallest = array1[0];

  for (int element : array1) {
      if (element < smallest) {
           smallest = element;
      }
   }
      return smallest;

   } else {
     return 0;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM