String arr[]={"1","2","3","4","5","6","7","8","9","10"}; 删除 字符串“1”,“3”,“5”,“7“返回一个新的数组
时间: 2023-08-31 16:08:29 浏览: 40
删除一个字符串中的数字后输出
可以使用循环和条件语句遍历原数组,将不需要删除的元素添加到一个新的数组中,最后返回新数组即可。示例代码如下:
```
String arr[]={"1","2","3","4","5","6","7","8","9","10"};
List<String> newArr = new ArrayList<>();
for(String str: arr){
if(!str.equals("1") && !str.equals("3") && !str.equals("5") && !str.equals("7")){
newArr.add(str);
}
}
String[] resultArr = newArr.toArray(new String[newArr.size()]);
System.out.println(Arrays.toString(resultArr));
```
输出结果为:[2, 4, 6, 8, 9, 10]
阅读全文