/** * 创建一个测试类 * * @class Info * @description * @author 李智慧 * @copyRight copyright(c) 2011 广东南航易网通电子商务有限公司,Rights Reserved * @time Nov 10, 2011 3:40:05 PM */ public class Student { private int age; private String name; private String sex; 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Student(int age, String name, String sex) { super(); this.age = age; this.name = name; this.sex = sex; } }
?
import java.util.ArrayList; import java.util.List; /** * 讲一个list转换成另外一个list * @class Into * @description * @author 李智慧 * @copyRight copyright(c) 2011 广东南航易网通电子商务有限公司,Rights Reserved * @time Nov 10, 2011 3:35:41 PM */ public class Into { public static void main(String[] args) { Into into = new Into(); List list = into.initList(); List list2 = into.changeList(list); into.printMsg(list2); } /** * 初始化对象 * @return */ public List initList(){ List list = new ArrayList<Student>(); /** 十个基本测试的对象学生 **/ Student student = new Student(21, "张三", "男"); Student student2 = new Student(24, "李四", "男"); Student student3 = new Student(23, "王五", "女"); Student student4 = new Student(24, "赵六", "女"); Student student5 = new Student(26, "韩琦", "男"); Student student6 = new Student(21, "尾巴", "男"); Student student7 = new Student(29, "哈哈", "男"); Student student8 = new Student(23, "弄弄", "女"); Student student9 = new Student(24, "章晓", "男"); Student student10 = new Student(21, "费力", "女"); list.add(student); list.add(student2); list.add(student3); list.add(student4); list.add(student5); list.add(student6); list.add(student7); list.add(student8); list.add(student9); list.add(student10); return list; } /** * 核心方法,转换list,如果年龄一样的话,叠加在一起 * @param list * @return */ public List changeList(List list) { List list2 = new ArrayList<Student>(); for(int i=0;i<list.size();i++){ Student student = (Student) list.get(i); addMsg(list2,student); } return list2; } /** * 加入对应的对象 * @param list * @param student */ public static void addMsg(List list,Student student){ if(list.size() == 0){ //如果是第一个直接加进去 list.add(student); }else{ for (int i = 0; i < list.size(); i++) { Student student2 = (Student) list.get(i); if(i == list.size()-1){ list.add(student); break; } if(student2.getAge() == student.getAge()){ if(student.getName().equals(student2.getName())){ }else{ //已经添加过了的 list.remove(i); Student student3 = new Student(student.getAge(),student.getName()+"/"+student2.getName(),student.getSex()+"/"+student2.getSex()); list.add(student3); break; } }else{ continue; } } } // return list; } /** * 打印出转换后的结果 */ public void printMsg(List list){ System.out.println("*****分类统计数据*****"); for(int i=0;i<list.size();i++){ Student student = (Student) list.get(i); System.out.print(student.getAge()+"\t"); System.out.print(student.getName()+"\t"); System.out.println(student.getSex()); } System.out.println("*****分类统计数据*****"); } }
??