如何检查一个空数组的Java [英] how to check for an empty array java

查看:190
本文介绍了如何检查一个空数组的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这段代码对于检查数组是否为空是否有效,还是应该检查null?

I wanted to know if this code is valid for checking whether an array is empty, or should I check for null?

if(arrayName={})
  System.out.println("array empty");
else System.out.println("array not empty");

谢谢!

推荐答案

在数组类中,我们有一个定义为"length"的静态变量,该变量保存数组对象中的元素数.您可以使用它来找到长度,如下所示:

In array class we have a static variable defined "length", which holds the numberof elements in array object. You can use that to find the length as:

if(arrayName.length == 0)
  System.out.println("array empty");
else 
  System.out.println("array not empty");

这篇关于如何检查一个空数组的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆