tz reur
tz reur

Reputation: 23

Checking for null element in an Array

I want to check whether an Array element is null.

I have initialized an array of String which has a size of 2. I looped through the array and check whether an array element is null. If it's null, I will add a String "a" to that position only.

My codes below will produce the following output:

1=a
2=a

Code:

public class CheckArrayElementIsNull {
    public static void main(String[] args) {
        String[] arr = new String[2];       
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] == null) {
                arr[i] = "a";
            }
            System.out.println(i + "=" + arr[i]);   
            if(arr[i] == null) {
                System.out.println(i + "= null");
            }
        }
    }
}

I tried to add a break after my if condition but is not printing out anything.

 public class CheckArrayElementIsNull {
    public static void main(String[] args) {
        String[] arr = new String[2];       
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] == null) {
                arr[i] = "a"; 
                break;
            }
            System.out.println(i + "=" + arr[i]);   
            if(arr[i] == null) {
                System.out.println(i + "= null");
            }
        }
    }
}

I expected this output:

1=a
2=null

Upvotes: 1

Views: 401

Answers (3)

Mustafa sabir
Mustafa sabir

Reputation: 4360

It is because break will break the iteration of the for loop, and exit out of loop immediately. You should change your design a bit. From what I understand you are trying to achieve (Assigning "a" to the first null element encountered and stop, print all elements in the array) following should work:-

public class CheckArrayElementIsNull {

  public static void main(String[] args) {
     String[] arr = new String[3];       
     boolean flag=true; //use a flag that will check if null is encountered
     for(int i = 0; i < arr.length; i++) {
       if(arr[i] == null && flag) { // first time flag will be true
         arr[i] = "a"; 
         flag=false; //set flag to false , to avoid further assignment of 'a' to null values
        }
        System.out.println(i + "="+ arr[i]);    //print array
        }
     }
  }

Upvotes: 0

Dragondraikk
Dragondraikk

Reputation: 1679

You have a few problems in your loop.

    for(int i = 1; i < arr.length - 1; i++) { //<---- This does not iterate over the entire array, leaving out the first and last elements
        if(arr[i] == null) {
            arr[i] = "a"; 
            break; //<---- This terminates the loop entirely, if you want to stop all instructions past this one try using continue instead
        }
        System.out.println(i + "=" + arr[i]);   
        if(arr[i] == null) { //This code is unreachable as arr[i] is initialized if it was detected as null before
            System.out.println(i + "= null");
        }else{
            System.out.println(i + "=" + arr[i]); 
        }
    }

Instead you should try

 for(int i = 0; i < arr.length; i++) {
    if(arr[i] == null) {
        arr[i] = "a";
        System.out.println(i + "= null");
        break;
    }
    System.out.println(i + "=" + arr[i]); 
}

Upvotes: 2

Stefan
Stefan

Reputation: 1433

Try assigning the value at the index you expect not to be null before you enter your for loop:

String[] arr = new String[3];
arr[1] = "a";
for (int i = 0; i < arr.length; i++)
{
  if (arr[i] == null)
  {
    System.out.println(i + "=null");
  }
  else
  {
    System.out.println(i + "=" + arr[i]);
  }
}

Upvotes: 0

Related Questions