String類
toCharArray() 方法將字符串轉換為字符數(shù)組。
無參數(shù)。
返回值是字符數(shù)組。
具體實例
劍指offer 11題:輸入一個整數(shù)镶殷,輸出該數(shù)二進制表示中1的個數(shù)。其中負數(shù)用補碼表示微酬。
public class Solution {
public int NumberOf1(int n) {
String str = Integer.toBinaryString(n);
char[] c = str.toCharArray();
int t = 0;
for(int i=0; i<c.length; i++){
if(c[i]=='1'){
t++;
}
}
return t;
}
}