public class may{
public static void main(String[] args) {
ArrayList<Person> al = new ArrayList<Person>();
al.add(new Person("张三", 18));
al.add(new Person("李四", 17));
al.add(new Person("王五", 19));
al.add(new Person("赵六", 18));
//删除集合中年龄为18的person(自己定义的person类)
for (int i = 0; i < al.size(); i++) {
if(al.get(i).getAge()==18){ //这个地方会报错,为什么啊?
al.remove(i);
}
}
}
}
然后要创建类Person.
public class Person {
public Person(String string, int i) {
// TODO Auto-generated constructor stub
}
public int getAge() {
// TODO Auto-generated method stub
return 0;
}
} |