- package com.actmaps;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * TODO
- * @author PHeHPHeH
- * Created On 2007-8-30 16:04:27
- */
- public class Test {
- public List init() {
- List list = new ArrayList();
- list.add(new Student("张三",13));
- list.add(new Student("是三",12));
- list.add(new Student("句三",14));
- list.add(new Student("李三",18));
- list.add(new Student("飞三",11));
- list.add(new Student("也三",17));
- list.add(new Student("去三",15));
- list.add(new Student("提三",12));
- list.add(new Student("李三",13));
- list.add(new Student("人三",18));
- list.add(new Student("他三",13));
- list.add(new Student("米三",15));
- list.add(new Student("平三",16));
- return list;
- }
- public int iterList(List list,IDoExecute tool){
- int result = 0;
- Student student = null;
- for(int i = 0; i < list.size(); i++) {
- student = (Student)list.get(i);
- result += tool.execute(student);
- }
- return result;
- }
- public int getTotalAge(List list) {
- return this.iterList(list,new DoExecute(){
- public int execute(Student stu){
- return stu.getAge();
- }
- });
- }
- public int getNum(List list) {
- return this.iterList(list,new DoExecute(){
- public int execute(Student stu){
- if(stu.getName().startsWith("李"))
- return 1;
- else
- return 0;
- }
- });
- }
- public static void main(String args[]) {
- Test test = new Test();
- List list = test.init();
- System.out.println(test.getTotalAge(list));
- System.out.println(test.getNum(list));
- }
- class Student{
- private String name = "";
- private int age = 0;
- public Student(String n,int a){
- name = n;
- age = a;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- interface IDoExecute{
- public int execute(Student stu);
- }
- class DoExecute implements IDoExecute{
- public int execute(Student stu){
- return 0;
- }
- }
- }