求大神,哪里错了?
package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; /** * 备选课程类 * @author Jack * */ public class ListTest { //用于存放备选课程 public List coursesToSelect; public ListTest(){ this.coursesToSelect = new ArrayList(); } //add courses public void testAdd(){ Courses cou1 = new Courses(); cou1.setId("1"); cou1.setName("数据结构"); coursesToSelect.add(cou1); Courses c = (Courses) coursesToSelect.get(0); System.out.println("添加了课程:"+c.getId()+" "+c.getName()); cou1.setId("2"); cou1.setName("高等数学"); coursesToSelect.add(cou1); Courses c1 = (Courses) coursesToSelect.get(1); System.out.println("添加了课程:"+c1.getId()+" "+c1.getName()); } public void testIterator(){ Iterator it = coursesToSelect.iterator(); while(it.hasNext()){ Courses cs = (Courses) it.next(); System.out.println(cs.getId()+" "+cs.getName()); } } public void testForEach(){ for(Object obj : coursesToSelect){ Courses co = (Courses)obj; System.out.println(co.getId()+" "+co.getName()); } } public static void main(String[] args){ ListTest lt = new ListTest(); lt.testAdd(); lt.testIterator(); lt.testForEach(); } }
输出如下: