0

I'm using a GUI and using lists. When the list is empty and they press the 'show list' button, instead of it just displaying "[]" I want to create an if statement that results in the message "your list is empty" instead. I have tried along the lines of this

if(!myList.contains(null)) { // problem here
        listBox.setText(myList.toString());
}else{
        listBox.setText("Your list is empty");
    }

I've also tried !myList.contains("") but that doesn't seem to work either. Any help much appreciated. Thanks.

puffa
  • 61
  • 1
  • 1
  • 6

2 Answers2

3

List has an isEmpty method that returns true if there are no items in the list.

 if (!myList.isEmpty()) { 
     listBox.setText(myList.toString());
 } else {
     listBox.setText("Your list is empty");
 }
bhspencer
  • 13,086
  • 5
  • 35
  • 44
1

The isEmpty() function should resolve your problem or maybe you could validate the size with lenght.

It depends on what you want to do, but you should trying to Override the toString() method.

How can I override the toString method of an ArrayList in Java?

Regards

Community
  • 1
  • 1