63

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 List is not empty. Users can enter numbers, -1 to stop the program. This is the code I now have, but this doesn't work, it always says 'List isn't empty'.

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 (-1 to stop)"));
            numbers.add(number);
        } while (number != -1);
        giveList(numbers);
    }

    public static void giveList(List<Integer> numbers)
    {
        if (numbers != null)
            JOptionPane.showMessageDialog(null, "List isn't empty");
        else
            JOptionPane.showMessageDialog(null, "List is empty!");
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
STheFox
  • 1,478
  • 4
  • 18
  • 24
  • 1
    The reason you are always getting the "List isn't empty" message is because you are checking after you instantiate it and add a number to it. Even if the user inputs -1, it still gets added to the list. – jonhopkins Jan 03 '13 at 18:53
  • 1
    `null` and an empty list are not the same thing. – SLaks Jan 03 '13 at 18:54
  • 1
    You [read the javadocs](http://docs.oracle.com/javase/7/docs/api/java/util/List.html#isEmpty(%29 ) ... however, it seems you don't understand what `null` means, which is a larger issue. – Brian Roach Jan 03 '13 at 18:55
  • @jonhopkings: but how to do so? I have to first instantiate my arraylist I guess like now. And my do-while makes sure the value -1 can't be inserted in the arraylist? – STheFox Jan 03 '13 at 18:57
  • @user1873613 replace `numbers.add(number);` with `if (number != -1) numbers.add(number);` This will prevent -1 being inserted into the list, and if -1 is the first thing the user enters, then the list will be empty when `giveList` is called – jonhopkins Jan 03 '13 at 18:59
  • @STheFox: For future readers: do while always executes at least once, so the -1 will be added, and only then checked. – ShadowFlame Apr 09 '18 at 07:37

6 Answers6

172

As simply as:

if (numbers.isEmpty()) {...}

Note that a quick look at the documentation would have given you that information.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 19
    it's better to use if ((null == numbers) && (numbers.isEmpty())) {...} – LiangWang Dec 16 '14 at 01:18
  • 2
    @Jacky is correct, either you guarantee initialization, or you MUST check for null, otherwise you are subject to a NullPointerException. – tony gil Dec 29 '16 at 11:12
  • 1
    @tonygil if the method is private then you should ensure null can't happen and the null check is not necessary. If it is public you can enforce the "non null" contract in the javadoc and with `Objects.requireNonNull(numbers)` at the start of the method (but it will throw a NPE too so doesn't buy you much). However, checking for null in the condition doesn't look right to me. See also: http://stackoverflow.com/questions/271526/avoiding-null-statements – assylias Dec 29 '16 at 13:17
19

You should use method listName.isEmpty()

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
4

Good practice nowadays is to use CollectionUtils from either Apache Commons or Spring Framework.

CollectionUtils.isEmpty(list))
FazoM
  • 4,777
  • 6
  • 43
  • 61
3

Your original problem was that you were checking if the list was null, which it would never be because you instantiated it with List<Integer> numbers = new ArrayList<Integer>();. However, you have updated your code to use the List.isEmpty() method to properly check if the list is empty.

The problem now is that you are never actually sending an empty list to giveList(). In your do-while loop, you add any input number to the list, even if it is -1. To prevent -1 being added, change the do-while loop to only add numbers if they are not -1. Then, the list will be empty if the user's first input number is -1.

do {
    number = Integer.parseInt(JOptionPane.showInputDialog("Enter a number (-1 to stop)"));
    /* Change this line */
    if (number != -1) numbers.add(number);
} while (number != -1);
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
1

Alternatively, you may also want to check by the .size() method. The list that isn't empty will have a size more than zero

if (numbers.size()>0){
//execute your code
}
J_fruitty
  • 47
  • 1
  • 1
  • 6
-3

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<Integer> list=new ArrayList<Integer>();

    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)