java 如何检查数组[]在java中是否有空值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31531332/
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-11-02 18:45:47  来源:igfitidea点击:

How to check if array[] has a null value in java?

javaarrays

提问by Ti J

I am reading a csv file. One of the requirements is to check if a certain column has a value or not. In this case I want to check the value in array[18]. However, I am getting

我正在阅读一个 csv 文件。要求之一是检查某个列是否具有值。在这种情况下,我想检查array[18]. 然而,我得到

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18

Is there any other way to check the array index if it has a value or empty?

有没有其他方法可以检查数组索引是否有值或为空?

My code:

我的代码:

while ((sCurrentLine = br.readLine())!= null) { 

    String[] record = sCurrentLine.split(",");   

    if(record.length > 0){ // checking if the current line is not empty on the file
       if(record[18] != null){ // as per console, I am getting the error in this line
          String readDateRecord = record[18];
          // other processing here
       }
    }
}

回答by rakeb.mazharul

Look, according to JavaSE7:

看,根据JavaSE7

ArrayIndexOutOfBoundsException thrown to indicate that an array has been accessed with an illegal index. (In your case) The index is greater than or equal to the size of the array.

抛出 ArrayIndexOutOfBoundsException 以指示已使用非法索引访问数组。(在您的情况下)索引大于或等于数组的大小。

Means, the index 18is not legal for array recordin your code. Moreover if the array recordis nullthen you will get another exception called NullPointerException.

意味着,索引18对于record代码中的数组是不合法的。此外,如果数组record是,null那么您将获得另一个名为NullPointerException.

To overcome your problem, there are many solutions, one can be

为了克服你的问题,有很多解决方案,一个可以

//assuming that record is initialized 
if(record.length > 18){
      String readDateRecord = record[18];
      ...
   }

回答by Akash Yellappa

If its an arrayList then check if it contains null

如果它是一个 arrayList 然后检查它是否包含 null

array.contains(null));

回答by Subodh Joshi

Here one approach is this

这是一种方法

Object array[] = new Object[18];
boolean isempty = true;
for (int i=0; i<arr.length; i++) {
  if (arr[i] != null) {
    isempty = false;
    break;
  }
}

回答by Razib

You may try the following code snippet -

您可以尝试以下代码片段 -

int length = record.length;   
if( (n>0 && n<length-1) && record[n] != null ){

   String readDateRecord = record[n];
   //other processing here

}

回答by xrcwrn

public static void main (String[] args) {
    Integer [] record = {1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1};
    for(int i = 0; i < record.length; i++)
        if(record[i] == null) {
            System.out.println(i+1 + " position is null");
        }
}

回答by Sunil

Size is fixed in array after creation .if you get index beyond of size then it's generated ArrayIndexOutOfBoundException.So first you need to get the size of array then retrive the value from array

创建后,大小在数组中固定。如果索引超出大小,则会生成它ArrayIndexOutOfBoundException。因此,首先您需要获取数组的大小,然后从数组中检索值

 int size=record.length;
 for(int i=0;i<size;i++)
  {
   if(record[i] != null){
     // other processing here
   }
 }

Declare an array with size “n” and access the n-th element. However, as we already mentioned, the indices of an array with size “n”, reside in the interval [0, n – 1].

声明一个大小为“n”的数组并访问第 n 个元素。然而,正如我们已经提到的,大小为“n”的数组的索引位于区间 [0, n – 1] 中。