How can a string or a set of chars consisting of 0 and 1 be converted to boolean ? It seems that in c ++ it was easy to do, because 0=false , the rest = true , and here the complexity

    1 answer 1

     char[] charArray = string.toCharArray(); boolean[] mass = new boolean[charArray.length]; for(int i=0; i<charArray.length; i++){ if (charArray[i] == '0') mass[i] = false; else mass[i] = true; } 

    somehow in the forehead


    It is possible without an array of characters

     boolean[] mass = new boolean[string.length]; for(int i=0; i<string.length; i++){ if (string.charAt(i) == '0') mass[i] = false; else mass[i] = true; } 

    A great option offered @diraria

     boolean[] mass = new boolean[string.length]; for(int i=0; i<string.length; i++){ mass[i] = string.charAt(i) != '0' ; } 
    • 3
      instead of if / else you can write mass[i] = charArray[i] == '1' - diraria
    • one
      excellent proposal, add to the response on your behalf =) just not == '1', should not be equal to 0. In accordance with the condition "0 = false, the rest = true" - Victor
    • The obvious is near, thanks)) - Arkady
    • one
      I would like to add that C ++ type String is represented as an array of chars, when in Java it is a separate type that has api different from api array - Slava Povazhnyuk