方法isEmpty()在Java中做什么?
如果此列表不包含任何元素,则类java.util.ArrayList 的 isEmpty()方法返回true。
示例
import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList<Integer> arrlist = new ArrayList<Integer>(5); arrlist.add(25); arrlist.add(10); arrlist.add(20); arrlist.add(35); boolean retval = arrlist.isEmpty(); if (retval == true) { System.out.println("list is empty"); } else { System.out.println("list is not empty"); } for (Integer number : arrlist) { System.out.println("Number = " + number); } } }
输出结果
list is not empty Number = 25 Number = 10 Number = 20 Number = 35