Joa*_*uer 21
是否char[]包含构成数字数字的unicode字符?在这种情况下,只需从中创建一个String char[]并使用Integer.parseInt:
char[] digits = { '1', '2', '3' };
int number = Integer.parseInt(new String(digits));
Run Code Online (Sandbox Code Playgroud)
Tha*_*lur 10
更高性能和更清晰的代码(并且不需要分配新的String对象):
int charArrayToInt(char []data,int start,int end) throws NumberFormatException
{
int result = 0;
for (int i = start; i < end; i++)
{
int digit = (int)data[i] - (int)'0';
if ((digit < 0) || (digit > 9)) throw new NumberFormatException();
result *= 10;
result += digit;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
小智 7
char[] digits = { '0', '1', '2' };
int number = Integer.parseInt(String.valueOf(digits));
Run Code Online (Sandbox Code Playgroud)
这也有效.
| 归档时间: |
|
| 查看次数: |
68301 次 |
| 最近记录: |