Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to see if a arraylist is filled or not

Tags:

java

Hello I am creating a application that uses arraylists ( practice purposes not real app ) I have created a method that gives me the answer of a math but only if the arraylist contains no object. for some reason I always see the else in my if/else construction.

Here is how I check if the array list contains objects

public void sluitRegistratie() {

    aantalBezoekers = bezoeker.size();

    if(!(aantalBezoekers >= 0)) {

        String str = "Gemiddelde tijd bezoekers: " + (gesommeerdeTijd / aantalBezoekers);

        JOptionPane.showMessageDialog(null, str);
    }
    else {
        JOptionPane.showMessageDialog(null, "Bezoekers zijn nog niet weg");
    }

}
like image 389
Reshad Avatar asked Dec 04 '22 01:12

Reshad


2 Answers

ArrayList has an isEmpty() method that will return true if the arraylist is empty, false otherwise. So it looks like you want if(bezoeker.isEmpty())...

like image 180
Vyassa Baratham Avatar answered Dec 05 '22 15:12

Vyassa Baratham


The size of an ArrayList can never be negative, so your check for !size()>=0 will never be true. Just check if size()==0.

like image 28
awolfe91 Avatar answered Dec 05 '22 15:12

awolfe91