如何检查数组是否为空?

时间:2022-09-06 11:13:54

I have written a program where if the array set is let's say {1, 3, 6, 7, 12}, it will return its minimum gap between two numbers. In other words, it will first find the differences between 3 and 1, 6 and 3, 7 and 6, and 12 and 7. After their differences are achieved, it will return the least difference, in our case 1, since 6-7=1. If we were given an array set of {60}, for example, the program will return 0. Now if we had an array set of {}, where nothing is inside, it will return 0 as well. However, I can't get my program to return a 0! It throws an exception. What did I miss? How should I solve this problem? Here is my program so far:

我编写了一个程序,如果数组设置是{1,3,6,7,12},它将返回两个数字之间的最小间隙。换句话说,它将首先找到3和1,6和3,7和6,以及12和7之间的差异。在达到它们的差异之后,它将返回最小的差异,在我们的情况1中,因为6-7 = 1。例如,如果给出了一个{60}的数组,程序将返回0.现在,如果我们有一个{}的数组,其中没有任何内容,它也会返回0。但是,我无法让我的程序返回0!它引发了一个例外。我错过了什么?我该如何解决这个问题?到目前为止,这是我的程序:

public static void main(String[] args) {
    int[] numberSet = {1, 3, 6, 7, 12};
    //int[] numberSet = {};
    System.out.println(minGap(numberSet));      
}

public static int minGap(int[] numberSet) {
    int[] differenceArray = new int[numberSet.length-1];
    int smallestNum = 0;
    if (numberSet.length < 2) {
        return 0;
    }
    else {
        for(int i = 0; i < numberSet.length-1; i++) {
            differenceArray[i] = numberSet[i+1] - numberSet[i]; 
        }       
        Arrays.sort(differenceArray);
        smallestNum = differenceArray[0];
        return smallestNum;
    }
}

Thanks in advance!

提前致谢!

4 个解决方案

#1


10  

Your test:

你的考试:

if (numberSet.length < 2) {
    return 0;
}

should be done before you allocate an array of that length in the below statement:

应该在以下语句中分配该长度的数组之前完成:

int[] differenceArray = new int[numberSet.length-1];

else you are already creating an array of size -1, when the numberSet.length = 0. That is quite odd. So, move your if statement as the first statement in your method.

否则你已经创建了一个大小为-1的数组,当numberSet.length = 0时。这很奇怪。因此,将if语句作为方法中的第一个语句。

#2


13  

To check array is null:

要检查数组是否为空:

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

To check array is empty:

要检查数组是否为空:

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

#3


2  

you may use yourArray.length to findout number of elements in an array.

您可以使用yourArray.length来查找数组中的元素数量。

Make sure yourArray is not null before doing yourArray.length, otherwise you will end up with NullPointerException.

在执行yourArray.length之前确保yourArray不为null,否则最终会出现NullPointerException。

#4


1  

Your problem is that you are NOT testing the length of the array until it is too late.

你的问题是你没有测试数组的长度,直到为时已晚。

But I just want to point out that the way to solve this problem is to READ THE STACK TRACE.

但我只想指出解决这个问题的方法是阅读堆栈跟踪。

The exception message will clearly tell you are trying to create an array with length -1, and the trace will tell you exactly which line of your code is doing this. The rest is simple logic ... working back to why the length you are using is -1.

异常消息将清楚地告诉您正在尝试创建长度为-1的数组,并且跟踪将告诉您代码的哪一行正是这样做的。其余的是简单的逻辑...回到你使用的长度为-1的原因。

#1


10  

Your test:

你的考试:

if (numberSet.length < 2) {
    return 0;
}

should be done before you allocate an array of that length in the below statement:

应该在以下语句中分配该长度的数组之前完成:

int[] differenceArray = new int[numberSet.length-1];

else you are already creating an array of size -1, when the numberSet.length = 0. That is quite odd. So, move your if statement as the first statement in your method.

否则你已经创建了一个大小为-1的数组,当numberSet.length = 0时。这很奇怪。因此,将if语句作为方法中的第一个语句。

#2


13  

To check array is null:

要检查数组是否为空:

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

To check array is empty:

要检查数组是否为空:

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

#3


2  

you may use yourArray.length to findout number of elements in an array.

您可以使用yourArray.length来查找数组中的元素数量。

Make sure yourArray is not null before doing yourArray.length, otherwise you will end up with NullPointerException.

在执行yourArray.length之前确保yourArray不为null,否则最终会出现NullPointerException。

#4


1  

Your problem is that you are NOT testing the length of the array until it is too late.

你的问题是你没有测试数组的长度,直到为时已晚。

But I just want to point out that the way to solve this problem is to READ THE STACK TRACE.

但我只想指出解决这个问题的方法是阅读堆栈跟踪。

The exception message will clearly tell you are trying to create an array with length -1, and the trace will tell you exactly which line of your code is doing this. The rest is simple logic ... working back to why the length you are using is -1.

异常消息将清楚地告诉您正在尝试创建长度为-1的数组,并且跟踪将告诉您代码的哪一行正是这样做的。其余的是简单的逻辑...回到你使用的长度为-1的原因。