Первый индекс устанавливается в NULL (пустой), но он не печатает правильный вывод, почему?
//set the first index as null and the rest as "High"
String a []= {null,"High","High","High","High","High"};
//add array to arraylist
ArrayList<Object> choice = new ArrayList<Object>(Arrays.asList(a));
for(int i=0; i<choice.size(); i++){
if(i==0){
if(choice.get(0).equals(null))
System.out.println("I am empty"); //it doesn't print this output
}
}
Я полагаю, то, что вы хотите сделать, это изменить,
if(choice.get(0).equals(null))
на
if(choice.get(0) == null))
Вы хотите:
for (int i=0; i<choice.size(); i++) {
if (i==0) {
if (choice.get(0) == null) {
System.out.println("I am empty"); //it doesn't print this output
}
}
}
Выражение choice.get (0) .equals (null)
должно вызывать исключение NullPointerException
, потому что choice.get (0)
равно null
, и вы пытаетесь вызвать на нем функцию. По этой причине anyObject.equals (null)
будет всегда возвращать false
.