Java 如何使用字符串替换方法而不替换为空字符串?
我正在从csv文件加载一个字符串,并试图用它生成一个int数组。问题是我一直遇到Java 如何使用字符串替换方法而不替换为空字符串?,java,arrays,string,replace,Java,Arrays,String,Replace,我正在从csv文件加载一个字符串,并试图用它生成一个int数组。问题是我一直遇到NumberFormatException,当程序在字符串数组中找到“时会抛出该异常 我不需要那些空字符串,我只需要整数 有没有办法避免用空字符串替换字符 您可以在解析之前过滤流中的not empty和notnull。像 final int[] scores = Arrays.stream(strScores) .filter(x -> x != null && !x.isEmp
NumberFormatException
,当程序在字符串数组中找到“
时会抛出该异常
我不需要那些空字符串,我只需要整数
有没有办法避免用空字符串替换字符
您可以在
解析
之前过滤流中的not empty和notnull
。像
final int[] scores = Arrays.stream(strScores)
.filter(x -> x != null && !x.isEmpty())
.mapToInt(Integer::parseInt)
.toArray();
您可以在解析
之前过滤流中的not empty和notnull
。像
final int[] scores = Arrays.stream(strScores)
.filter(x -> x != null && !x.isEmpty())
.mapToInt(Integer::parseInt)
.toArray();
您应该使用数组列表,因为您可以轻松地添加和删除
array_list<String> list = new ArrayList<String>(); // create array list
for (int i =0; i < array_string.length;i++){
if (!string_array[i].equals("")){ // filter out empty
array_list.add(string_array[i]);
}
}
String new_string_array_without_empty = array_list.toArray(new String[0]); // convert to string array again
array_list list=new ArrayList();//创建数组列表
for(int i=0;i
您应该使用数组列表,因为您可以轻松地添加和删除
array_list<String> list = new ArrayList<String>(); // create array list
for (int i =0; i < array_string.length;i++){
if (!string_array[i].equals("")){ // filter out empty
array_list.add(string_array[i]);
}
}
String new_string_array_without_empty = array_list.toArray(new String[0]); // convert to string array again
array_list list=new ArrayList();//创建数组列表
for(int i=0;i
谢谢,我不知道你能做到。谢谢,我不知道你能做到。谢谢,但我不想添加任何不需要的数据结构。你说得对。我对安卓系统的开发还不熟悉,但我决定无论如何都要尽力帮忙。谢谢你的回复。:)谢谢,但我不想添加任何我不需要的数据结构。你说得对。我对安卓系统的开发还不熟悉,但我决定无论如何都要尽力帮忙。谢谢你的回复。:)