java - 为什么 list.get(0).equals(null) 不起作用?

标签 java list null arraylist

第一个索引设置为 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))

关于java - 为什么 list.get(0).equals(null) 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2556978/

相关文章:

java - 如何使用偏移 Recycler View 进行自动平滑滚动?

java - 使用要创建的类参数化 JAXB 工厂方法

java - Netty 的 ChannelBuffer.read 出现奇怪的 indexOutOfBound 错误?

java - Spring mcvc 应用程序中未选取 Log4j2 属性

javascript - 使用 Angular JS 比较两个列表并删除公共(public)元素

java - 为什么java字符串方法将null作为字符串返回?

list - sencha touch::list 未向右滚动

java - 从列表中的类访问类方法

javascript - 获取动态生成的下拉菜单Jquery的值

java - null 是非声明变量吗?