Java-Ternary operator

public boolean contains(Object[] array, Object obj) {
    for (Object a : array) {
        a.equals(obj) ? return true : return false;
    }
}

How does the ternary operator work in java? what's wrong with the condition?

Author: diralik, 2017-06-06

4 answers

public boolean contains(Object[] array, Object obj) {
    for (Object a : array) {
        a.equals(obj) ? return true : return false;
    }
}

First, we urgently seal the degenerate case with an empty array. Otherwise, there is an execution path in which the value is not returned for a non-void function (even if it is really unreachable, the compiler does not know it):

public boolean contains(Object[] array, Object obj) {
    for (Object a : array) {
        a.equals(obj) ? return true : return false;
    }
    return false;
}

Second, the ternary operator expects three expressions: for a condition and two branches. And return does not form an expression. The current context (in which the result could be used) it certainly terminates, so it doesn't make sense to be by an expression, its result cannot be used anywhere. Since you have both branches returning something, why not "put return out of brackets"?

public boolean contains(Object[] array, Object obj) {
    for (Object a : array) {
        return a.equals(obj) ? true : false;
    }
    return false;
}

Third, a.equals(obj) already returns boolean (true or false). Why is there a ternary operator at all?

public boolean contains(Object[] array, Object obj) {
    for (Object a : array) {
        return a.equals(obj);
    }
    return false;
}

Syntactic correctness is achieved. Then you'll figure it out.
But I still have one more point.

Fourth, this function lives up to its name only for arrays of length no more than 1. Document this is the behavior. Or fix it if it's not what you intended.

 8
Author: , 2017-06-06 22:42:26

There is another way, for me more readable

Arrays.asList(array).contains(obj)

I believe that the ternary operator should be used very carefully. It often generates subtle and very serious errors.

Update The asList method returns a wrapper object over an array(without additional memory overhead) with the correct implementations of the List interface methods for arrays.

 3
Author: Ruslan Masgutov, 2017-06-06 22:52:35

Mlin, 3 answers about 7 options and only 1 correct and the one was pecked as the apogee of shit-hacking

People, what are you doing?

public boolean contains(Object[] array, Object obj) {
    for (Object a : array) {
       if(a==null && obj==null)
          return true;
       if(a==null)
          continue;
       if(a.equals(obj))
         return true;
    }
    return false;
}
 2
Author: Barmaley, 2017-06-07 05:17:36

First, it can be easier:

public boolean contains(Object[] array, Object obj) {
    for (Object a : array) {
        return a.equals(obj);
    }
    return false;
}

Secondly, if you really want to use a ternary operator, then the syntax is lame, and you need to do this:

public boolean contains(Object[] array, Object obj) {
    for (Object a : array) {
        return a.equals(obj) ? true : false;
    }
    return false;
}

PS. Both methods return false if array is empty.

PPS. I did not take into account the logic of the method (and it is not true), I corrected only the ternary operator.

 1
Author: post_zeew, 2017-06-07 06:54:08