Converting Boolean to Integer in Java without If-Statements

后端 未结 12 1960
盖世英雄少女心
盖世英雄少女心 2020-12-30 02:26

I\'m wondering if there\'s a way to convert a boolean to an int without using if statements (as not to break the pipeline). For example, I could write

int bo         


        
12条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 03:15

    A reasonable alternative to ising to the ternary to avoid an "if":

    private static Boolean[] array = {false, true};
    
    int boolToInt( boolean b ){
        return Arrays.binarySearch(array, b);
    }
    

    Note that I consider this s "puzzle" question, so if coding it myself i would use the ternary..

提交回复
热议问题