赞
踩
Person类:
public class Person { private String name; private int age; private String sex; private String address; public Person(){ } public Person(String name, int age, String sex, String address) { this.name = name; this.age = age; this.sex = sex; this.address = address; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Homework类:
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class Homework { public static void main(String[] args) { List list = new ArrayList(); Person p = new Person("张三",23,"男","河南"); list.add(p); list.add(new Person("李四",45,"男","河南")); list.add(new Person("王五",34,"女","山西")); list.add(new Person("赵六",22,"男","上海")); list.add(new Person("田七",33,"女","北京")); ListIterator it = list.listIterator(); while (it.hasNext()){ Person per =(Person)it.next(); String name = per.getName(); if ("李四".equals(name)){ it.add(new Person("zhao6",24,"女","广州")); } if ("王五".equals(name)){ it.remove(); } } for (Object obj:list){ System.out.println(obj); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。