20172332 2017-2018-2 《程序設計與數據結構》第九周學習總結
阿新 • • 發佈:2018-05-13
層次結構 處理異常 nal streams 驗證 nag 過多 相同 pen
20172332 2017-2018-2 《程序設計與數據結構》第九周學習總結
教材學習內容總結
- 1.異常處理
- 異常拋出的問題
- 處理異常的方法
- 2.未捕獲的異常。
- 3.try-catch語句。
- 沒有異常,將繼續執行try語句後的語句塊,所有catch子句後的語句。
- 有異常,則控制立刻轉移到相應的catch子句處理異常。
- 4.finally子句。
- 一條try-catch語句可以有一個可選的finally子句,用於定義一段無論是否有異常發生都將執行的代碼。
- 如果有finally子句,則必須跟在所有的catch子句後面。
- try語句塊可以沒有catch子句,但仍然可以使用finally子句。
- finally語句與return語句的關系:
- ①如果finally語句中沒有return語句,先執行finally語句中的代碼,然後在執行try語句塊或catch語句塊中的return語句;
- ②若finally語句中有return語句,則finally語句中的return語句覆蓋其他語句塊中的語句,最後返回的是finally中的return語句。
- 5.異常的傳遞。(就與傳遞性一樣,舉個例子:程序一存在異常,程序二調用程序一,程序二就會存在與程序一相同的異常。)
- 6.異常類層次結構。(許多類型的異常類都由Exception類派生,但定義各種異常的子類卻分散定義在其他幾個包中,繼承關系可以跨越包邊界。)
- 7.自定義異常。(說白了就是自己寫代碼,定義一種異常)
- 8.可檢測異常與不可檢測異常。
- 可檢測異常必須由方法捕獲,或者必須在可能拋出或傳遞異常方法的throws子句中列出來。
- throws子句用在方法頭。throw子句用在方法中。
- java中唯一的不可檢測異常是RuntimeException類的對象或該類的後代類對象。
- 9.I/O異常(其中還有老師上課講的把數據寫入文本,和從文本中讀取數據內容)
- 三種標準I/O流(in、out、err)
- 可檢測的IOExceptions異常
- 10.遞歸思想(一個調用自己的方法)
- 11.無窮遞歸(類似於無限循環)
- 12.數學中的遞歸運算(階乘和求和)。
- 13.遞歸與叠代的比較:
- 所有的問題都可以用循環叠代的方法求解,但某些情況下循環叠代會很復雜,遞歸方法可以寫出更簡潔而精良的程序。
- 14.直接遞歸和簡介遞歸。
- 方法調用自己的遞歸——直接遞歸
- 方法調用其他方法,最終導致再次調用自己——間接遞歸。
- 15.遞歸的應用:
- ①迷宮旅行。
- ②漢諾塔問題。
教材學習中的問題和解決過程(其實現在老師開始先講然後在讓我們學的模式之後,教材上的問題是愈發的少了= =,不知道寫什麽)
問題1:這種在字符串中的索引值和長度是怎麽算的
問題1解決方案:索引值和長度都不算入字符串內。(寫個小程序驗證了一下)
代碼調試中的問題和解決過程
問題1:
問題1解決方案:StackOverflowError是由於當前線程的棧滿了 ,也就是函數調用層級過多導致。改為
問題2:pp11.2中為什麽字符串的索引值要輸20,按理說,字符串第一個索引值為0,所以第20個字符的索引值應是19。
問題2解決方案:emmmm,這是我想錯了,輸出第20個字符以後的,應該從21開始算起,所以索引值應為20。
問題3:pp12.1中
問題3解決方案:索引值越界了,因為我的字符串變了,索引值也跟著變,就會出問題。應該變成字符串不變,索引值變。
代碼托管
很奇怪我的代碼量變少了!不過已經上5000了,就不統計了吧!讓代碼量就定格在那一瞬間!
上周考試錯題總結
1.A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception?
- A . ignore the exception
- B . handle the exception where it arose using try and catch statements
- C . propagate the exception to another method where it can be handled
- D . throw the exception to a pre-defined Exception class to be handled
- E . all of the above are ways that a Java program could handle an exception
- 答案:D ;我選的:E
- 分析:異常不會被拋出異常類。
- 單詞:1.handle:處理。2.exception:異常。3.exception:傳送。
2.An exception can produce a "call stack trace" which lists
- A . the active methods in the order that they were invoked
- B . the active methods in the opposite order that they were invoked
- C . the values of all instance data of the object where the exception was raised
- D . the values of all instance data of the object where the exception was raised and all local variables and parameters of the method where the exception was raised
- E . the name of the exception thrown
- 答案:B ;我選的:D
- 分析:棧是最先放的最後出來,所以順序是相反的。
- 單詞:1.invoke:調用。2.parameters:參數。
3.Character streams manage
- A . byte-sized data
- B . binary data
- C . Unicode characters
- D . ASCII characters
- E . compressed data
- 答案:C ;我選的:B
- 分析:字符流用於管理16位的Unicode字符
- 單詞:1.Character:字符。2.compressed:壓縮。
4.PrintWriter is a better output stream class that PrintStream because PrintWriter
- A . has both print and println methods and PrintStream only has print
- B . can output both byte and character streams and PrintStream can only output byte streams
- C . has error checking mechanisms as part of the class and PrintStream does not
- D . will not throw checked exceptions and PrintStream will
- E . all of the above
- 答案:C ;我選的:B
- 分析:PrintWriter類是一個Writer類,而PrintStream類是一個流類。主要的區別在於PrintWriter是專門為文件而設計的,因此有錯誤檢查機制而不是PrintStream的一部分。(意思就是說PrintWriter比PrintStream好在有錯誤檢查機制)
- 單詞:1.mechanisms:機制。
5.The following defines a new Exception called AnewException.
public Exception ANewException { public ANewException(String message) { super(message); } }
- A . true
- B . false
- 答案:B ;我選的:A
- 分析:自定義異常必須定位為類而不是異常
- 單詞:1.exception:異常。
點評過的同學博客和代碼
- 本周結對學習情況
- 20172326
- 20172313
- 結對學習內容
- 第十一章異常,第十二章遞歸。
- 上周博客互評情況
- 20172326
- 20172313
其他(感悟、思考等,可選)
- 大部分知識老師在課上已經講了,還擴展了內容,所以學起來並不費勁。
學習進度條
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一周 | 182/182 | 1/1 | 10/10 | |
第二周 | 458/640 | 1/2 | 15/25 | |
第三周 | 469/1109 | 2/4 | 18/43 | 學會IDEA的使用和調試,學會jdb調試。 |
第四周 | 1536/2645 | 1/5 | 24/67 | |
第五周 | 980/3625 | 1/6 | 25/92 | |
第六周 | 870/4495 | 1/7 | 16/108 | |
第七周 | 455/4950 | 2/9 | 22/130 | |
第八周 | 1322/6272 | 2/11 | 28/158 | |
第九周 | 2/13 | 28/186 |
計劃學習時間:20小時
實際學習時間:28小時
改進情況:課後的pp除了pp12.9我認為有難度以外,別的還好,大部分時間依舊花在了四則運算的結對編程中。
參考資料
- 《Java程序設計與數據結構教程(第八版)》
- finally語句與return語句的關系
20172332 2017-2018-2 《程序設計與數據結構》第九周學習總結