如何使用java删除集合中的'元素呢?下面是小编给大家提供的删除集合中元素的常见方法,欢迎阅读,更多详情请关注应届毕业生考试网。
Java代码如下:
package ;
import yList;
import ator;
import ;
/**
* <b>Description</b>:
* <p>
* <b>Features or change log:</b>
* <ol>
* <li>2014年1月21日 上午10:58:21, jervalj, C001:</li>
* <li></li>
* </ol>
*/
public class TestMain {
public static void main(String[] args) {
removeItem1();
removeItem2();
removeItem3();
}
private static void removeItem1() {
List<Integer> list = getList();
// 使用迭代器
Iterator<Integer> iter = ator();
Integer item = null;
while (ext()) {
item = ();
if (null != item && 5 == alue()) {
ve();
}
}
tln(list);
}
private static void removeItem2() {
List<Integer> list = getList();
// 手动索引操作
int len = ();
Integer item = null;
for (int i = 0;i < len;i++) {
item = (i);
if (null != item && 5 == alue()) {
ve(item);
i--;
len--;
}
}
tln(list);
}
private static void removeItem3() {
List<Integer> list = getList();
// 使用引用克隆
List<Integer> listCopy = new ArrayList<Integer>();
ll(list);
for (Integer item:list) {
if (null != item && 5 == alue()) {
ve(item);
}
}
tln(listCopy);
}
private static List<Integer> getList() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0;i < 10;i++) {
(i);
}
return list;
}
}
Java代码运行结果:
[0, 1, 2, 3, 4, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 6, 7, 8, 9]