问答详情
源自:4-6 学生选课—课程查询

报string异常

public void testGet() {

int size = coursesToSelect.size();

System.out.println("有如下课程待选:");

for(int i= 0 ; i < size; i++) {

Course cr=(Course)coursesToSelect.get(i);

System.out.println("课程:" + cr.id + ":" + cr.name);

}

}


private void testForEach() {

System.out.println("有如下课程待选(通过for each访问):");

Course course=new Course();

for (Object obj : coursesToSelect) {

Course cr =(Course) obj;

System.out.println("课程:" + cr.id + ":" + cr.name);




Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to com.imooc.collection.Course

at com.imooc.collection.ListTest.testGet(ListTest.java:127)

at com.imooc.collection.ListTest.main(ListTest.java:118)


提问者:慕移动0989271 2017-05-05 13:19

个回答

  • 慕移动0989271
    2017-05-05 13:29:48

    就是他导致的异常

  • 慕移动0989271
    2017-05-05 13:28:56

    找到了

    public static void main( String[] args) {

    ListTest lt = new ListTest();

    lt.testAdd();

    // lt.testType();

    lt.testForEach();

    lt.testGet();

    lt.testIterator();

    lt.testForEach();

    lt.testModify();

    lt.testForEach();

    lt.testRemove();

    }

    把上面的testtype也注释了