How to check if a list is empty in java

How to check if a list is empty in java,in case list is empty in ArraryList() then code should provide prompt or log messages in console in order to verify list is empty or having any data in list.Below is the sample code to verify List is empty or not.

Sample Code:


import java.util.*;
import javax.swing.JOptionPane;

public class ArrayListEmpty 
{
    public static void main(String[] args) 
    {
        List<Integer> numbers = new ArrayList<Integer>();
        int number;
        do {
            number = Integer.parseInt(JOptionPane.showInputDialog("Enter a number)"));
            numbers.add(number);
        } while (number != -1);
        giveList(numbers);
    }

    public static void giveList(List<Integer> numbers)
    {
        if (numbers.isEmpty())
            JOptionPane.showMessageDialog(null, "List is empty");
        else
            JOptionPane.showMessageDialog(null, "List is not empty");
    }
}

No comments:

Post a Comment