Java ArrayList - Check if list is empty

前端 未结 6 885
小蘑菇
小蘑菇 2020-12-05 13:00

How can I check if a list is empty? If so, the system has to give a message saying List is empty. If not, the system has to give a message saying Li

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 13:09

    Source: CodeSpeedy Click to know more Check if an ArrayList is empty or not

    import java.util.ArrayList;
    public class arraycheck {
    public static void main(String args[]){
    ArrayList list=new ArrayList();
    
        if(list.size()==0){
            System.out.println("Its Empty");
    
        }
        else
            System.out.println("Not Empty");
    
       }
    
    }
    

    Output:

    run:
    Its Empty
    BUILD SUCCESSFUL (total time: 0 seconds)
    

提交回复
热议问题