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
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' ; } - 3instead of
if/elseyou can writemass[i] = charArray[i] == '1'- diraria - oneexcellent 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
- oneI 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
|