java 检查arraylist是否不为空java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28444739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:36:23  来源:igfitidea点击:

check if arraylist is NOT empty java

javaarraylist

提问by Ahmad Ghazi Altarabsheh

I know the isEmpty() method used to check if an arraylist is empty, but I am trying to check if an arraylist is not empty. I tried to look online but I didn't find any useful information on how to do this. My code is like "while ArrayList is not empty then run code).

我知道用于检查数组列表是否为空的 isEmpty() 方法,但我正在尝试检查数组列表是否为空。我试图在网上查看,但没有找到有关如何执行此操作的任何有用信息。我的代码就像“虽然 ArrayList 不为空,然后运行代码)。

回答by David Merinos

Invert the result of isEmpty().

反转 的结果isEmpty()

public boolean notEmpty(ArrayList a) {
    return !a.isEmpty();
}

That will tell you when a list is not empty.

这将告诉您列表何时不为空。

回答by J_fruitty

Alternatively, you can also check whether the array is null by the length/size of the arraylist.

或者,您也可以通过数组列表的长度/大小检查数组是否为空。

while(arrayListName.size() > 0 ){
//execute code
}

回答by Stuart Cardall

If you initialize arrays as nullyou can just check if they are not null:

如果你初始化数组,null你可以检查它们是否不是null

List<String> myArray = null;

myArray = myFunction.getArrayValues;

if (myArray != null) {
  processArray (myArray);
}

回答by Klassic_Pegg

This is easier to read for me

这对我来说更容易阅读

while (arrayList.isEmpty() == false) {
  //do something cool
}