最近遇到一个问题,比较有意思的是要删除String[]中的多个元素,而要删除元素的数组是double[] 型的:
String items[] = {"1","2","3","4","5"};
double[] need_del_items = {2,3};
完整代码:
String items[] = {"1","2","3","4","5"};
for(String a:items)
System.out.println(a);
double[] need_del_items = {2,3};
List<String> list1=Arrays.asList(items);
List<String> arrList = new ArrayList<String>(list1); //这句话的重要性在上一节blog中有讲到
/* 主要是下面两行 */
java.text.NumberFormat NF = java.text.NumberFormat.getInstance();
//设置数的小数部分所允许的最小位数,避免小数位有多余的0
NF.setMinimumFractionDigits(0);
for(double a:need_del_items)
{
String b = NF.format(a);
arrList.remove(b);
}
arrList.toArray();
System.out.println(arrList);
运行结果:
1
2
3
4
5
[1, 4, 5]