Skip to content

Latest commit

 

History

History
49 lines (38 loc) · 1.11 KB

File metadata and controls

49 lines (38 loc) · 1.11 KB

Java ArrayList isEmpty()方法

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

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

public boolean isEmpty()

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

package beginnersbook.com;
import java.util.ArrayList;
public class IsEmptyExample {
  public static void main(String args[]) {
      //ArrayList of Integer Type
      ArrayList<Integer> al = new ArrayList<Integer>();
      //Checking whether the list is empty
      System.out.println("Is ArrayList Empty: "+al.isEmpty());

      //Adding Integer elements
      al.add(1);
      al.add(88);
      al.add(9);
      al.add(17);

      //Again checking for isEmpty
      System.out.println("Is ArrayList Empty: "+al.isEmpty());

      //Displaying elements of the list
      for (Integer num: al) {
      System.out.println(num);
   }
}
}

输出:

Is ArrayList Empty: true
Is ArrayList Empty: false
1
88
9
17