Java中ArrayList的isEmpty()方法用于检查列表是否为空。如果列表不包含任何元素,则返回true;否则,如果列表包含任何元素,则返回false。
用法:
list_name.isEmpty()
参数:它不接受任何参数。
返回:如果列表list_name没有元素,则返回True,否则返回false。返回类型为boolean数据类型。
错误和异常:此方法没有错误或异常。
演示isEmpty()在Java中工作的程序:
// Java code to demonstrate the working of 
// isEmpty() method in ArrayList 
  
// for ArrayList functions 
import java.util.ArrayList; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // creating an Empty Integer ArrayList 
        ArrayList<Integer> arr = new ArrayList<Integer>(10); 
  
        // check if the list is empty or not using fucntion 
        boolean ans = arr.isEmpty(); 
        if (ans == true) 
            System.out.println("The ArrayList is empty"); 
        else
            System.out.println("The ArrayList is not empty"); 
  
        // addition of a element to the ArrayList 
        arr.add(1); 
  
        // check if the list is empty or not 
        ans = arr.isEmpty(); 
        if (ans == true) 
            System.out.println("The ArrayList is empty"); 
        else
            System.out.println("The ArrayList is not empty"); 
    } 
}输出:
The ArrayList is empty The ArrayList is not empty
相关用法
- Java LinkedHashSet isEmpty()用法及代码示例
- Java HashSet isEmpty()用法及代码示例
- Java NavigableSet isEmpty()用法及代码示例
- Java ConcurrentHashMap isEmpty()用法及代码示例
- Java Map isEmpty()用法及代码示例
- Java AbstractSequentialList isEmpty()用法及代码示例
- Java BitSet isEmpty()用法及代码示例
- Java LinkedTransferQueue isEmpty()用法及代码示例
- Java Hashtable isEmpty()用法及代码示例
- Java CopyOnWriteArraySet isEmpty()用法及代码示例
- Java CopyOnWriteArrayList isEmpty()用法及代码示例
- Java Vector isEmpty()用法及代码示例
- Java ArrayDeque isEmpty()用法及代码示例
- Java HashMap isEmpty()用法及代码示例
注:本文由纯净天空筛选整理自Striver大神的英文原创作品 ArrayList isEmpty() in Java with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
