Почему не делает list.get (0) .equals (пустая) работа?

Первый индекс устанавливается в 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
    }
}
7
задан Pyrolistical 31 March 2010 в 22:47
поделиться

2 ответа

Я полагаю, то, что вы хотите сделать, это изменить,

if(choice.get(0).equals(null))

на

if(choice.get(0) == null))
7
ответ дан 6 December 2019 в 15:20
поделиться

Вы хотите:

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 .

7
ответ дан 6 December 2019 в 15:20
поделиться
Другие вопросы по тегам:

Похожие вопросы: