I have this code snippet that is basically checking for a 0 value in a ArrayList of long integers.
import java.io.*;
import java.util.*;
class main
{
public static void main (String[] args) throws java.lang.Exception
{
long zero = 0;
ArrayList<Long> alist = new ArrayList();
alist.add (zero);
if (alist.contains(0))
System.out.println ("contains zero");
else
System.out.println ("contains no zero");
}
}
and the output I get is contains no zero. Rather unnerving!
Is this a bug or the expected behavior?
Change this logical check to: alist.contains(0L). This behavior is related to auto boxing/unboxing. int is boxed to Integer, and long is boxed to Long. In your list you put a long (boxed to a Long), then you search for an int (boxed to an Integer)... so you don't find it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With