オブジェクトの部分的な nfilled 配列を持っていて、それを反復処理するときに、選択したオブジェクトが null
であるかどうかをチェックしてから他の処理を行おうとしました。しかし、null
かどうかをチェックする行為でさえ、NullPointerException
を通過するようです。また、array.length
はすべてのnull
要素を含んでしまいます。配列の null
要素のチェックはどのように行うのでしょうか?例えば、以下のコードでは、NPEをスローします。
Object[][] someArray = new Object[5][];
for (int i=0; i<=someArray.length-1; i++) {
if (someArray[i]!=null) { //do something
}
}
あなたが言った以上のことが起こっているのです。 あなたの例から、次のような拡張テストを実行しました。
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]の長さをチェックしようとしているのでしょうか?
ありません。
以下を参照してください。 投稿したプログラムは想定どおりに実行されます。
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>
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要素を見つける場合は、上記のようにループを使用する必要があります。 .
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;
}