String[]转Integer[]
时间: 2023-10-01 14:10:02 浏览: 31
可以使用循环将String数组中的每个元素转换为Integer类型并存储在Integer数组中。以下是一个示例代码:
```java
String[] stringArray = {"1", "2", "3", "4", "5"};
Integer[] integerArray = new Integer[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
integerArray[i] = Integer.parseInt(stringArray[i]);
}
```
在上面的代码中,我们首先创建了一个String数组`stringArray`,其中包含一些字符串。然后,我们创建了一个相同大小的Integer数组`integerArray`来存储转换后的整数值。
接下来,我们使用循环迭代String数组中的每个元素,并使用`Integer.parseInt()`方法将其转换为整数类型。然后,将转换后的整数值存储在Integer数组的相应位置。
完成循环后,`integerArray`将包含与`stringArray`相同位置的整数值。
相关问题
String 转Integer
把一个 String 转成 Integer 的方法可以使用 Integer.parseInt(),例如:
```
String str = "123";
int num = Integer.parseInt(str);
```
这样,字符串 "123" 就被转换成了整数类型的 123。需要注意的是,如果字符串中包含的不是数字,那么调用 parseInt() 方法就会抛出一个 NumberFormatException 异常。
string 转 integer
可以使用Integer.parseInt(String s)方法将字符串s转换为整数类型的Integer。例如:
```java
String s = "123";
int i = Integer.parseInt(s);
```
这样i的值就是123。如果字符串s不是一个合法的整数,例如包含非数字字符,那么该方法将会抛出NumberFormatException异常。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)