string translate to array int
时间: 2023-10-24 17:34:52 浏览: 46
To convert a String to an array of integers in Java, you can use the following code:
```
String str = "12345";
int[] arr = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
arr[i] = Character.getNumericValue(c);
}
```
In this code, we first create a String `str` with the value we want to convert to an array of integers. We then create an integer array `arr` with the same length as the String `str`.
We use a `for` loop to iterate over each character in the String `str`. For each character, we use the `charAt()` method to get the character at the current index, and then use the `Character.getNumericValue()` method to convert the character to an integer. We then assign this integer value to the corresponding index in the integer array `arr`.
After the loop completes, the integer array `arr` will contain the integer values corresponding to the characters in the String `str`.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)