Java ArrayList - 检查列表是否为空 [英] Java ArrayList - Check if list is empty
本文介绍了Java ArrayList - 检查列表是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查列表是否为空?如果是这样,系统必须给出一个消息说列表为空.如果不是,系统必须给出一个消息,说列表不为空.用户可以输入数字,-1
来停止程序.这是我现在拥有的代码,但这不起作用,它总是说列表不是空的".
How can I check if a list is empty? If so, the system has to give a message saying List is empty. If not, the system has to give a message saying List is not empty.
Users can enter numbers, -1
to stop the program.
This is the code I now have, but this doesn't work, it always says 'List isn't empty'.
import java.util.*;
import javax.swing.JOptionPane;
public class ArrayListEmpty
{
public static void main(String[] args)
{
List<Integer> numbers = new ArrayList<Integer>();
int number;
do {
number = Integer.parseInt(JOptionPane.showInputDialog("Enter a number (-1 to stop)"));
numbers.add(number);
} while (number != -1);
giveList(numbers);
}
public static void giveList(List<Integer> numbers)
{
if (numbers != null)
JOptionPane.showMessageDialog(null, "List isn't empty");
else
JOptionPane.showMessageDialog(null, "List is empty!");
}
}
推荐答案
就像:
if (numbers.isEmpty()) {...}
请注意,快速浏览文档 会给你这些信息.
Note that a quick look at the documentation would have given you that information.
这篇关于Java ArrayList - 检查列表是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文