正在尝试检查int[]是否为空Java

正在尝试检查int[]是否为空Java,java,list,is-empty,Java,List,Is Empty,很简单,我试图检查int[]距离是否为空 为什么这不起作用 我的代码: int[] distances = {3,6,7,6,1,8,8,2,3,4,5,9}; if(distances != null && !distances.isEmpty()) { throw new TransportException("No routes in entry"); } else { return distances;

很简单,我试图检查int[]距离是否为空

为什么这不起作用

我的代码:

int[] distances = {3,6,7,6,1,8,8,2,3,4,5,9};

    if(distances != null && !distances.isEmpty()) {
        throw new TransportException("No routes in entry");
    }
        else {

    return distances;
    }
在简单数组上没有可以调用的
isEmpty()
方法。只需将其长度与0进行比较

if (distances == null || distances.length == 0)

除了在
Java.lang.Object
上声明的方法外,Java数组没有任何方法

它们唯一的属性是
length
,即数组的长度。所以你需要

if (distances != null && distances.length > 0)
阅读有关数组的文章。或者读一本介绍Java的书

if(distances != null && !distances.isEmpty()) 

条件确保:“当我的数组不为null且包含一些值时引发异常。因此,只要数组不为空,它就会引发异常。我认为这不是您想要的。

返回错误找不到符号-metho为空()请阅读关于数组类型的教程。假设当数组为
null
或为空时,他想进入
if
子句的主体,这是行不通的。好吧,他只需要切换if和else主体。这是OP应该能够自己找到的bug,而这不是问题所在。