我正在使用 GUI 并使用列表。当列表为空并且他们按下“显示列表”按钮时,我想创建一个 if 语句,而不是仅显示“[]”,该语句会导致消息“您的列表为空”。我已经尝试过这样做
if(!myList.contains(null)) { // problem here
listBox.setText(myList.toString());
}else{
listBox.setText("Your list is empty");
}
我也尝试过 !myList.contains("") 但这似乎也不起作用。非常感谢任何帮助。谢谢。
最佳答案
List 有一个 isEmpty 方法,如果列表中没有项目,该方法将返回 true。
if (!myList.isEmpty()) {
listBox.setText(myList.toString());
} else {
listBox.setText("Your list is empty");
}
关于java - if语句条件防止空列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584688/