Java ArrayList isEmpty()方法

java.util.ArrayList.isEmpty() 判断ArrayList是否为空,如果为空(没有元素),则返回true。

1 语法

public boolean isEmpty()

2 参数

3 返回值

如果ArrayList为空(没有元素),则返回true。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.ArrayList.isEmpty()方法的例子
 */
import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {

        // create an empty array deque with an initial capacity
        ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

        // use add() method to add elements in the deque
        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");
        }

        // printing all the elements available in list
        for (Integer number : arrlist) {
            System.out.println("Number = " + number);
        }
    }
}

输出结果为:

list is not empty
Number = 25
Number = 10
Number = 20
Number = 35

 

热门文章

优秀文章