java 如何知道ArrayList中指定的对象是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6568662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:24:09  来源:igfitidea点击:

How to know if specified object in ArrayList is null?

javanullarraylist

提问by Dominik

I want to know if an Object in an ArrayList is null. If it's null, then it shouldn't do anything. Example:

我想知道 ArrayList 中的对象是否为空。如果它为空,那么它不应该做任何事情。例子:

if(!(theList.get(theIndexofObject) == null)){
   do something...
}
else{
   do nothing...
}

This doesn't work, because it throws an exception cause of the '.get()'-method. Any ideas to solve this problem?

这不起作用,因为它引发了 '.get()' 方法的异常原因。有什么想法可以解决这个问题吗?

回答by Alex

Use the contains()Method of your list:

使用contains()列表中的方法:

boolean contains(Object o)

回答by Adriaan Koster

You are probably confused about how to use the API. Here is a simple example of how it works:

您可能对如何使用 API 感到困惑。这是它如何工作的一个简单示例:

import java.util.ArrayList;
import java.util.List;

public class NullItems {
    public static void main(String[] args) {

        List<Object> items = new ArrayList<Object>();
        items.add("foo");
        items.add(null);
        items.add(25);

        for (int i = 0; i < items.size(); i++) {
            Object item = items.get(i);
            if (item != null) {
                System.out.println(item);
            }
        }

        // or shorter:
        for (Object item : items) {
            if (item != null) {
                System.out.println(item);
            }
        }
    }
}

回答by adamjmarkham

You are using the getmethod wrong. You need to pass the index an item is at to the getmethod. You could use the containsmethod to see if the object is in the ArrayList.

你使用的get方法不对。您需要将项目所在的索引传递给该get方法。您可以使用该contains方法查看对象是否在 ArrayList 中。

Example:

例子:

if(theList.contains(theObject))
   //do something

Otherwise you could use a try and catch which seems confusing and hard to read so I would strongly not recommend doing the following but have included it to show you:

否则,您可以使用看起来令人困惑且难以阅读的 try 和 catch,因此我强烈不建议您执行以下操作,但已将其包含在内以向您展示:

for(int i=0; i<theList.size(); i++)
{
    try
    {
       if(!(theList.get(i) == null))
       {
           //do something
       }
       else
       {
           //do nothing
       }
    }
    catch(NullPointerException npe)
    {
        //do something else
    }
}

Alternatively use a for-each loop.

或者使用 for-each 循环。

回答by Warrior

In javaScript itemArray.length, for java u have to use ARRAY.size() insted of length function

在 javaScript itemArray.length 中,对于 java,你必须使用长度函数的 ARRAY.size() 插入

 var itemArray=//Assign some list of value;
    for (var i = 0; i < itemArray.length; i++){

          if(itemArray[i].value == null){
             Do nothing
              }else{
                 Do something
              }
    }

回答by Sina

i think that your arraylist is null chang first condition to:

我认为你的 arraylist 是 null 改变第一个条件:

if(theList!=null && !(theList.get(theIndexofObject) == null)){
    // do something...
}
else{
    // do nothing...
}

回答by Muhammad Aamir Ali

The method arrayList.size() returns the number of items in the list - so if the index is greater than or equal to the size(), it doesn't exist.

方法 arrayList.size() 返回列表中的项目数 - 因此,如果索引大于或等于 size(),则它不存在。

回答by user3571695

if(!(theList.get(theIndexofObject) == null)){
   do something...
}
else{
   do nothing...
}

instead of writing this code.Try in the below format,I think you will get answer

而不是写这个代码。试试下面的格式,我想你会得到答案

if(theList.get(theIndexofObject)!= null)){
   do something...
}
else{
   do nothing...
}