2009-01-08 122 views
19

我有一个部分未充满的对象数组,当我遍历它们时,我试着检查选定的对象是否为null,然后再执行其他操作。但是,即使是通过NullPointerException来检查它是否为nullarray.length也将包括所有的null元素。你如何去检查数组中的null元素?例如在下面的代码中会为我提供一个NPE。如何检查数组元素是否为null,以避免Java中的NullPointerException

Object[][] someArray = new Object[5][]; 
for (int i=0; i<=someArray.length-1; i++) { 
    if (someArray[i]!=null) { //do something 
    } 
} 
+1

您的代码不给我一个NPR。您可能还想使用“i d0k 2009-01-08 19:11:00

+0

如果您检查`!someArray.equals(null)`,它会抛出NPE。 – bancer 2010-11-08 22:45:04

回答

22

你比你说的还多。我跑从比如下面的扩展测试:

public class test { 

    public static void main(String[] args) { 
     Object[][] someArray = new Object[5][]; 
     someArray[0] = new Object[10]; 
     someArray[1] = null; 
     someArray[2] = new Object[1]; 
     someArray[3] = null; 
     someArray[4] = new Object[5]; 

     for (int i=0; i<=someArray.length-1; i++) { 
      if (someArray[i] != null) { 
       System.out.println("not null"); 
      } else { 
       System.out.println("null"); 
      } 
     } 
    } 
} 

,并得到了预期的输出:

$ /cygdrive/c/Program\ Files/Java/jdk1.6.0_03/bin/java -cp . test 
not null 
null 
not null 
null 
not null 

你可能想检查的someArray [index]的长度?

+1

我也没有NPE。 – 2009-01-08 19:09:18

1

给定的代码适用于我。请注意,someArray [i]始终为空,因为您尚未初始化数组的第二维。

1

那么,首先,代码不会编译。

在i ++之后删除多余的分号后,它编译并运行正常。

6

它没有。

见下文。您发布的程序按假定运行。

C:\oreyes\samples\java\arrays>type ArrayNullTest.java 
public class ArrayNullTest { 
    public static void main(String [] args) { 
     Object[][] someArray = new Object[5][]; 
      for (int i=0; i<=someArray.length-1; i++) { 
       if (someArray[i]!=null) { 
        System.out.println("It wasn't null"); 
       } else { 
        System.out.printf("Element at %d was null \n", i); 
       } 
      } 
    } 
} 


C:\oreyes\samples\java\arrays>javac ArrayNullTest.java 

C:\oreyes\samples\java\arrays>java ArrayNullTest 
Element at 0 was null 
Element at 1 was null 
Element at 2 was null 
Element at 3 was null 
Element at 4 was null 

C:\oreyes\samples\java\arrays> 
1

示例代码不会抛出NPE。 (也不应该有一个';'后面的i ++)

0

战斗是否编译代码我会说创建一个数组6e 5添加2值并打印它们,你会得到两个值和其他为空。问题是尽管大小是5,但数组中有2个对象。如何找到阵列中有多少物体

2
String labels[] = { "MH", null, "AP", "KL", "CH", "MP", "GJ", "OR" }; 

if(Arrays.toString(labels).indexOf("null") > -1) { 
    System.out.println("Array Element Must not be null"); 
        (or) 
    throw new Exception("Array Element Must not be null"); 
}   
------------------------------------------------------------------------------------------   

For two Dimensional array 

String labels2[][] = {{ "MH", null, "AP", "KL", "CH", "MP", "GJ", "OR" },{ "MH", "FG", "AP", "KL", "CH", "MP", "GJ", "OR" };  

if(Arrays.deepToString(labels2).indexOf("null") > -1) { 
    System.out.println("Array Element Must not be null"); 
       (or) 
    throw new Exception("Array Element Must not be null"); 
}  
------------------------------------------------------------------------------------------ 

same for Object Array  

String ObjectArray[][] = {{ "MH", null, "AP", "KL", "CH", "MP", "GJ", "OR" },{ "MH", "FG", "AP", "KL", "CH", "MP", "GJ", "OR" };  

if(Arrays.deepToString(ObjectArray).indexOf("null") > -1) { 
    System.out.println("Array Element Must not be null"); 
       (or) 
    throw new Exception("Array Element Must not be null"); 
    } 

如果您想查找某个特定的null元素,则应如上所述使用for循环。

0
public static void main(String s[]) 
{ 
    int firstArray[] = {2, 14, 6, 82, 22}; 
    int secondArray[] = {3, 16, 12, 14, 48, 96}; 
    int number = getCommonMinimumNumber(firstArray, secondArray); 
    System.out.println("The number is " + number); 

} 
public static int getCommonMinimumNumber(int firstSeries[], int secondSeries[]) 
{ 
    Integer result =0; 
    if (firstSeries.length !=0 && secondSeries.length !=0) 
    { 
     series(firstSeries); 
     series(secondSeries); 
     one : for (int i = 0 ; i < firstSeries.length; i++) 
     { 
      for (int j = 0; j < secondSeries.length; j++) 
       if (firstSeries[i] ==secondSeries[j]) 
       { 
        result =firstSeries[i]; 
        break one; 
       } 
       else 
        result = -999; 
     } 
    } 
    else if (firstSeries == Null || secondSeries == null) 
     result =-999; 

    else 
     result = -999; 

    return result; 
} 

public static int[] series(int number[]) 
{ 

    int temp; 
    boolean fixed = false; 
    while(fixed == false) 
    { 
     fixed = true; 
     for (int i =0 ; i < number.length-1; i++) 
     { 
      if (number[i] > number[i+1]) 
      { 
       temp = number[i+1]; 
       number[i+1] = number[i]; 
       number[i] = temp; 
       fixed = false; 
      } 
     } 
    } 
    /*for (int i =0 ;i< number.length;i++) 
    System.out.print(number[i]+",");*/ 
    return number; 

} 
0

你可以做到这一点的一行代码(没有数组声明):

object[] someArray = new object[] 
{ 
    "aaaa", 
    3, 
    null 
}; 
bool containsSomeNull = someArray.Any(x => x == null); 
相关问题