string数组转integer数组
时间: 2023-04-26 09:04:47 浏览: 66
将一个字符串数组转换为整数数组,可以使用循环遍历字符串数组,然后使用Integer.parseInt()方法将每个字符串转换为整数,最后将整数添加到新的整数数组中。具体实现如下:
```
String[] strArray = {"1", "2", "3", "4", "5"};
int[] intArray = new int[strArray.length];
for (int i = ; i < strArray.length; i++) {
intArray[i] = Integer.parseInt(strArray[i]);
}
```
相关问题
string数组转integer数组的方法
可以使用Java中的循环和Integer.parseInt()方法来将每个字符串转换为整数,然后存储在新的整数数组中,例子如下:
String[] strArr = {"123", "456", "789"};
int[] intArr = new int[strArr.length];
for(int i = 0; i < strArr.length; i++){
intArr[i] = Integer.parseInt(strArr[i]);
}
Java string数组转换成integer list
可以使用Java 8中的流(Stream)来实现将String数组转换为Integer列表(List<Integer>)。具体代码如下:
```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StringArrayToIntList {
public static void main(String[] args) {
String[] stringArray = {"1", "2", "3", "4", "5"};
List<Integer> intList = Arrays.stream(stringArray)
.map(Integer::parseInt)
.collect(Collectors.toList());
System.out.println(intList);
}
}
```
输出结果为:`[1, 2, 3, 4, 5]`。
首先,我们使用Arrays.stream()方法将String数组转换为Stream对象,然后使用map()方法将每个String元素转换为对应的Integer元素,最后使用collect()方法将Stream对象转换为List对象。其中,Integer::parseInt是一个方法引用,用于将String转换为Integer。
相关推荐
![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)
![](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)