原文: https://beginnersbook.com/2013/12/java-arraylist-isempty-method-example/

java.util.ArrayList类的isEmpty()方法用于检查列表是否为空。此方法返回一个布尔值。

public boolean isEmpty()

如果列表为空则返回true,否则返回false

  1. package beginnersbook.com;
  2. import java.util.ArrayList;
  3. public class IsEmptyExample {
  4. public static void main(String args[]) {
  5. //ArrayList of Integer Type
  6. ArrayList<Integer> al = new ArrayList<Integer>();
  7. //Checking whether the list is empty
  8. System.out.println("Is ArrayList Empty: "+al.isEmpty());
  9. //Adding Integer elements
  10. al.add(1);
  11. al.add(88);
  12. al.add(9);
  13. al.add(17);
  14. //Again checking for isEmpty
  15. System.out.println("Is ArrayList Empty: "+al.isEmpty());
  16. //Displaying elements of the list
  17. for (Integer num: al) {
  18. System.out.println(num);
  19. }
  20. }
  21. }

输出:

  1. Is ArrayList Empty: true
  2. Is ArrayList Empty: false
  3. 1
  4. 88
  5. 9
  6. 17