如何检查列表是否为空?如果是这样,系统必须给出一条消息,说List是空的.如果没有,系统必须给出一条消息,说明列表不为空.用户可以输入数字,-1
以停止该程序.这是我现在的代码,但这不起作用,它总是说'List is not 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!");
}
}
Run Code Online (Sandbox Code Playgroud)
ass*_*ias 166
简单如下:
if (numbers.isEmpty()) {...}
Run Code Online (Sandbox Code Playgroud)
请注意,快速查看文档可以为您提供该信息.