亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

關于 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)




正在回答

1 回答

你是沒寫testForEach()方法

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


1 回復 有任何疑惑可以回復我~
#1

神不在的二月 提問者

非常感謝!
2016-06-06 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Java入門第三季
  • 參與學習       409769    人
  • 解答問題       4543    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進入課程

關于 It.testForEach();方法報錯

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號