本帖最后由 张甲甲 于 2013-5-27 10:04 编辑
我在一个ArraylistList里面加入了几个学生对象 我想删除其中的女学生
可是老是出异常
请大家帮着看看 我是菜鸟 真心求指点
代码如下:
package com.itheima;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
public class Test{
public static void main(String args[]) throws IOException
{
ArrayList al=new ArrayList();
al.add(new Student('男',20,"zhang1"));
al.add(new Student('女',21,"zhang2"));
al.add(new Student('男',22,"zhang3"));
al.add(new Student('女',22,"zhang4"));
Iterator it=al.iterator();
while(it.hasNext()){
Student s=(Student) it.next();
if(s.sex=='女'){
al.remove(s);
}
}
}
}
class Student {
char sex;
int age;
@Override
public String toString() {
return "Student [sex=" + sex + ", age=" + age + ", name=" + name + "]";
}
String name;
public Student(char sex, int age, String name) {
super();
this.sex = sex;
this.age = age;
this.name = name;
}
}
|