Java的ArrayList的 - 检查列表是空的 [英] Java ArrayList - Check if list is empty
本文介绍了Java的ArrayList的 - 检查列表是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何检查是否一个列表是空的?如果是这样,系统有给一个消息,说的列表为空。如果不是这样,系统有给一个消息,说的列表不为空。
用户可以输入数字, 1
来停止程序。
这是code我现在有,但是这并不工作,它总是说'列表不为空。
进口的java.util。*;
进口javax.swing.JOptionPane中;公共类ArrayListEmpty
{
公共静态无效的主要(字串[] args)
{
清单<整数GT;数=新的ArrayList<整数GT;();
INT编号;
做{
数=的Integer.parseInt(JOptionPane.showInputDialog(输入号码(-1停止)));
numbers.add(数);
}而(数字!= -1);
giveList(数字);
} 公共静态无效giveList(列表<整数GT;数字)
{
如果(!(numbers.isEmpty()))
JOptionPane.showMessageDialog(NULL,列表不为空);
其他
JOptionPane.showMessageDialog(NULL,列表是空的!);
}
}
解决方案
由于只是为:
如果(numbers.isEmpty()){...}
请注意,一个快速浏览一下文档会给你相关信息。
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.isEmpty()))
JOptionPane.showMessageDialog(null, "List isn't empty");
else
JOptionPane.showMessageDialog(null, "List is empty!");
}
}
解决方案
As simply as:
if (numbers.isEmpty()) {...}
Note that a quick look at the documentation would have given you that information.
这篇关于Java的ArrayList的 - 检查列表是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文