0

I have an array with 4 objects, and only one of them is not null. I want to call a method, giving that variable as a param.

Block[] b = getIntersectingBlocks(e); // I get 4 Block variables, and only one of them is not null
// ...
Listener.myMethod(notNullBlock); // notNullBlock should be the block out of the array, that is not null

What is the fastest way of doing that, and avoiding if - else statements like this:

if (b[0] != null) {
    Listener.myMethod(b[0]);
} else if (b[2] != null) {
    Listener.myMethod(b[1]);
}
// ...
6
  • 4
    How do you check if something is not null? Also, an object cannot be null. An array variable (an array element) can reference null. Commented Jun 11, 2014 at 3:39
  • Sorry -_- I meant variable xD Commented Jun 11, 2014 at 3:42
  • why not just use a for/in loop? then you would only need one if Commented Jun 11, 2014 at 3:47
  • Is there any particular reason the code is structured to return an empty array save for a single element? Commented Jun 11, 2014 at 3:47
  • why not put the whole null check thing inside one big if() with an OR condition Commented Jun 11, 2014 at 3:48

4 Answers 4

3

This doesn't use multiple if and will work for an arbitrary length array:

for (Block block : b)
    if (block != null)
        Listener.myMethod(block);

If more than one Block can be non-null, add a break to prevent the listener being called more than once:

for (Block block : b) {
    if (block != null) {
        Listener.myMethod(block);
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If understand your question, you could do it with a massive nested ternary (please don't do this in real code),

final Block notNullBlock = (b == null || b.length != 4) ? null :
    b[0] != null ? b[0] : b[1] != null ? b[1] : b[2] != null ? b[2] :
    b[3];

You could also use a for-each loop (but it requires an if) -

Block notNullBlock = null;
for (Block block : b) {
  if (block != null) {
    notNullBlock = block;
    break;
  }
}

1 Comment

I was almost going there too, but there are still tests in there, and I don't think it answers the question. I believe he's looking for a general solution that works for an arbitrary length array.
1

Use for each loop

for(Block bl:bArray){
   if(bl !=null)
    Listener.myMethod(b1);
}

Comments

1

Let say you have an array of Block as below: Block[] blocksArray = {null, null, new Block(), null};

Pass above array to this method:

    private Block provideBlock(Block[] blocksArray)   { 
        for(Block block : blocksArray) {
            block instanceof Block? return block : continue;
        }
    }

Then use returned not null block in your code as:

Block block = provideBlock(blocksArray);
Listener.myMethod(block); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.