NumberList是一个顺序容器,以下代码执行后,NumberList里的元素依次为:
List<int> NumberList = new List<int>(){2,4,1,3,5}; for(int i = 0;i<NumberList.Count;++i) { int v = NumberList[i]; if(v%2 = = 0) { NumberList.Remove(v);//删除的是元素,而非下标 } }
List<Integer> NumberList = new ArrayList<>(Arrays.asList(2,4,1,3,5)); for (int i = 0; i < NumberList.size(); i++) { int v = NumberList.get(i); if (v % 2 == 0) { NumberList.remove(i); } } System.out.println(NumberList.toString());