问答详情
源自:4-7 学生选课—课程修改

关于 It.testForEach();方法报错

  1. package com.imocc.collection;


  2. import java.util.Arrays;

  3. import java.util.Iterator;

  4. import java.util.List;

  5. import java.util.ArrayList;


  6. public class ListTest {

  7.   //用于存放备选课程的List

  8.   public java.util.List coursesToSelect;  //cousesToSelect是List数组的名字


  9.   public ListTest(){ //主类的构造方法

  10.  this.coursesToSelect = new ArrayList();//用于往coursesToSelect中添加备选课程

  11.   }


  12.     public void testAdd() {

  13.  Course cr1 = new Course("1","数据结构"); //还没有创建这个类

  14.  coursesToSelect.add(cr1);

  15.  Course temp = (Course)coursesToSelect.get(0);

  16.  System.out.println("添加了课程"+temp.id+":"+temp.name);

  17.  Course cr2 = new Course("2","C语言"); //还没有创建这个类

  18.  coursesToSelect.add(0,cr2); //怎么和上面的不一样?是由两个add方法吗?

  19.  Course temp2 = (Course)coursesToSelect.get(0);

  20.  System.out.println("添加了课程"+temp2.id+":"+temp2.name);

  21.  

  22.       Course[] course = {new Course("3","离散数学"),new Course("4","汇编语言")};

  23.       coursesToSelect.addAll(Arrays.asList(course));

  24.       Course temp3 = (Course)coursesToSelect.get(2);

  25.       Course temp4 = (Course)coursesToSelect.get(3);     

  26.       System.out.println("添加了两门课程:"+temp3.id +":"+ temp3.name+

  27.      ";"+temp4.id + ":" + temp4.name);

  28.       

  29.       Course[] course2 = {new Course("5","高等数学"),new Course("6","大学英语")};

  30.       coursesToSelect.addAll(2,Arrays.asList(course2));

  31.       Course temp5 = (Course)coursesToSelect.get(2);

  32.       Course temp6 = (Course)coursesToSelect.get(3);     

  33.       System.out.println("添加了两门课程:"+temp5.id +":"+ temp5.name+

  34.      ";"+temp6.id + ":" + temp6.name);  

  35.   

  36.     }

  37.     

  38.     /**

  39.      * 取得List中元素的方法

  40.      * @param args

  41.      */

  42. public void testGet(){

  43. int size = coursesToSelect.size();

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

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

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

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

  48. }

  49. }

  50. /*

  51. * 通过迭代器来遍历List

  52. */

  53. public void testIterator() {

  54. Iterator it = coursesToSelect.iterator();

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

  56.      while(it.hasNext()){

  57.      Course cr = (Course) it.next();

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

  59.      

  60.      

  61.      }

  62. }

  63. //修改List中元素

  64. public void testModify(){

  65. coursesToSelect.set(4,new Course("7","毛概"));

  66. }


  67. public static void main(String[] args) {

  68. ListTest It = new ListTest();

  69.    It.testAdd();  //想添加课程但是在ListTest中没有这个方法!

  70.    It.testGet();

  71.    It.testIterator();

  72.    It.testForEach();

  73.    It.testModify();

  74.    It.testForEach();

  75. }





编译的时候出现以下错误信息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 

The method testForEach() is undefined for the type ListTest

The method testForEach() is undefined for the type ListTest

Syntax error, insert "}" to complete ClassBody


  1. at com.imocc.collection.ListTest.main(ListTest.java:87)




提问者:神不在的二月 2016-06-05 17:36

个回答

  • lsnFor
    2016-06-05 18:54:35
    已采纳

    你是没写testForEach()方法

    public void testForeach(){
    		System.out.println("通过For each方法打印输出:");
    		for(Object c:coursesToSelect){
    			Course cr = (Course)c;
    			System.out.println("所有课程:"+cr.id+":"+cr.name);
    		}
    	}