-1

I do not have a way of really testing this without completely the other code I am working on but I just wanted to confirm this. If I am trying to code this statement:

If a node contains only positives, set decision to true. 
If it contains only negatives set it to false

where pos is an ArrayList of all of the positive examples of a node and neg is othe same but with all of the negatives. Would this statement be valid?

 if(!node.pos.isEmpty() && node.neg.isEmpty())
     node.decision = true;
 if(!node.neg.isEmpty() && node.pos.isEmpty())
     node.decision = false;

So pretty much I am wondering if i put that the ArrayList is != null then does that mean that it must contain at least 1 thing and then when I put == null then that means it is empty?

Azmir
  • 9
  • 2

1 Answers1

1

No. An ArrayList is an object and will only be null if it hasn't been instantiated using the new keyword.

Use ArrayList.isEmpty() or ArrayList.size() > 0.

Matt
  • 2,063
  • 1
  • 14
  • 35
  • 4
    Don't use `ArrayList.size() > 0.` for checking if a collection is empty. You should use the `isEmpty()` method. – rdas Apr 14 '19 at 19:33
  • @DroidX86 agreed, but figured it made sense to make him/her aware that the method exists. – Matt Apr 14 '19 at 19:34