代码很简单 就是添加一个索引 我愣是半天没想出来;
先创建元素
@Test public void test() { Student student1 = new Student("zhangsan", 1, 80); Student student2 = new Student("lisi", null, 90); Student student3 = new Student("wangwu", 2, 100); List<Student> list = new ArrayList<>(); list.add(student1); list.add(student2); list.add(student3); Iterator<Student> iterator = list.iterator(); int i = 0; while (iterator.hasNext()) { Student infoDb = iterator.next(); Student student4 = new Student("wang", 3, 1000000); if (infoDb.a.equals("wangwu")){ list.set(i, student4); }else if (infoDb.a.equals("zhangsan")){ iterator.remove(); } i++; System.out.println(infoDb.toString()); } System.out.println(list); }
//随便建个实体 public class Student { public Student(String a, Integer b, Integer c) { this.a = a; this.b = b; this.c = c; } public Student() { } String a; Integer b; Integer c; }
这样既可以删除又可以修改