




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、Leetcode151題目詳解第一章線性表此類題目考察線性表的操作,例如數(shù)組,單鏈表,雙向鏈表Remove Duplicates from Sorted Array描述Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For
2、 example, Given input array A = 1,1,2,Your function should return length = 2, and A is now 1,2.分析無代碼:public class Solution public int removeDuplicates(int A) if(A=null|A.length=0) return 0; int index=0; for(int i=1;iA.length;i+) if(Aindex!=Ai) A+index=Ai; return index+1; 相關題:Remove Duplicates from S
3、orted Array II 見1.2Remove Duplicates from Sorted Array II 描述Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array A = 1,1,1,2,2,3,Your function should return length = 5, and A is now 1,1,2,2,3分析可以加一個變量來記錄重復元素的個數(shù)能很好的解決問題。由于此題已經(jīng)是排序的了,如果是沒有排序的,可
4、以使用hashmap來求元素的個數(shù)。代碼public class Solution public int removeDuplicates(int A) if(A=null|A.length=0) return 0; int index=0,cnt=0; for(int i=1;iA.length;i+) if(Aindex!=Ai) A+index=Ai; cnt=0; else if(Aindex=Ai&cnt1) A+index=Ai; cnt+; return index+1; 相關題擴展該題到可重復n次的場景,只需要將cnt的上限設為Afirst的情況。代碼public int se
5、arch(int A, int target) if(A=null|A.length=0) return -1; int first=0,last=A.length-1; while(first=Afirst) if(target=Afirst&targetAmid&target=Alast) first=mid+1; else last=mid-1; return -1; 相關題目Search in Rotated Sorted Array II,見 1.4Search in Rotated Sorted Array II描述Follow up for ”Search in Rotated
6、Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.分析問題就出在邊界上,即Amid=Afirst的情況,如果這樣,那么無法確定mid的位置屬于上題的圖一還是圖二。因此這時判斷Afirst=target,如果不成立則first+縮小一個范圍。代碼public class Solution public boolean s
7、earch(int A, int target) if(A=null|A.length=0) return false; int first=0,last=A.length-1; while(firstAfirst) if(target=Afirst&targetAmid) last=mid-1; else first=mid+1; else if(AmidAmid&targetBk/2 遞歸找k-k/2個數(shù),且b的start位置為k/2+1Ak/2lenb)return findMedianCore(B,A,bstart,bend,astart,aend,k);if(lenaBbstart?
8、Bbstart:Aastart;int pa=k/2lena?lena:k/2;int pb=k-pa;if(Aastart+pa-1=Bbstart+pb-1)return Aastart+pa-1;else if(Aastart+pa-1Bbstart+pb-1)return findMedianCore(A,B,astart,aend,bstart+pb,bend,k-pb);elsereturn findMedianCore(A,B,astart+pa,aend,bstart,bend,k-pa);相關題目無Longest Consecutive Sequence描述Given an
9、unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given 100, 4, 200, 1, 3, 2, The longest consecutive elements sequence is 1,2, 3, 4. Return its length: 4. Your algorithm should run in O(n) complexity.分析此題由于復雜度是O(n)因此不能排序。因此想到用hash??紤]數(shù)組當hash但是由于題目
10、中說是整型,因此不能用。所以使用HashSet解決。將所有的數(shù)放入集合中(已經(jīng)去重)。取數(shù),前后探索,記錄連續(xù)的個數(shù)更新max。當set為空時返回的max即為最大值。本題在實驗室發(fā)現(xiàn)一個問題,即你在遍歷集合的時候修改集合會報錯,java.util.ConcurrentModificationException,因此我采用了HashMap來做,value記錄是否訪問過。代碼import java.util.Map.Entry;public class Solution public int longestConsecutive(int num) if(num=null) return 0; Ha
11、shMap set=new HashMap(); for(int i=0;inum.length;i+) set.put(numi,false); int max=0; /traversal IteratorEntry it=set.entrySet().iterator(); while(it.hasNext() Entry tem=it.next(); if(tem.getValue()=true) continue; int key=tem.getKey(); int cnt=1; tem.setValue(true); while(set.get(-key)!=null) cnt+;
12、set.put(key,true); key=tem.getKey(); while(set.get(+key)!=null) cnt+; set.put(key,true); max=maxcnt?max:cnt; return max; 但是這個方法過于繁瑣,我后來看了我最開始寫的代碼,發(fā)現(xiàn)我的處理方式是修改完以后每次遍歷之前另外使用it.iterator();重新獲取遍歷對象。代碼2public class Solution public int longestConsecutive(int num) if(num=null) return 0; HashSet set=new Hash
13、Set(); for(int i=0;inum.length;i+) set.add(numi); Iterator it=set.iterator(); int count=0; int max=0; while(it.hasNext() int a=it.next(); count+; set.remove(a); int tem=a; while(set.contains(+tem) count+; set.remove(tem); tem=a; while(set.contains(-tem) count+; set.remove(tem); if(countmax) max=coun
14、t; count=0; it=set.iterator(); return max; 這個代碼寫的時候我又犯了錯誤,即兩個循環(huán)一個向前查找一個向后查找,向前查找完以后要將tem值重新回到中間位置再向后查找。Two Sum描述Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the tar
15、get, whereindex1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.You may assume that each input would have exactly one solution.Input: numbers=2, 7, 11, 15, target=9Output: index1=1, index2=2分析方法1:暴力,每兩個元素都考慮到,這樣就有O(n2)復雜度方法2:哈希,很簡單。方法3:先排
16、序再夾逼,但是順序亂了index也亂了,因此不好。最終采用方法2的思路。代碼public class Solution public int twoSum(int numbers, int target) / HashMap map=new HashMap(); for(int i=0;inumbers.length;i+) map.put(numbersi,i); int index1=0,index2=0; for(int i=0;inumbers.length;i+) index1=i; int find=target-numbersindex1; if(map.get(find)!=n
17、ull&map.get(find)!=index1) index2=map.get(find); break; int res=new int2; res0=index1index2?index1+1:index2+1; return res; 相關題目3Sum,1.83Sum,1.94Sum,1.103Sum描述Given an array S of n integers, are there elements a,b,c in S such that a + b + c = 0? Find all unique triplets in the array which gives the s
18、um of zero.Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a b c) The solution set must not contain duplicate triplets.For example, given array S = -1 0 1 2 -1 -4.A solution set is:(-1, 0, 1)(-1, -1, 2)分析利用two sum的夾逼方法,先對數(shù)組進行排序。然后兩層循環(huán),第一層遍歷每個元素,然后找到后面數(shù)組中兩個元素和等于該元素。但是要考慮去重復的
19、問題。在第二層循環(huán)里,碰到相同元素就繼續(xù)向前。第一層循環(huán)碰到相同元素也繼續(xù)向前,這樣可以去重復。代碼public class Solution public ListList threeSum(int num) ListList res=new ArrayListList(); if(num=null|num.length=0) return res; Arrays.sort(num);/from small to big num for(int i=0;inum.length;i+) ArrayList sub=new ArrayList(); if(i0&numi=numi-1) cont
20、inue; int n1=numi; int sum=-n1; int p=i+1,q=num.length-1; while(pq) if(numq+nump=sum) sub.add(n1); sub.add(nump); sub.add(numq); res.add(ArrayList)sub.clone(); sub=new ArrayList(); while(p+1p&numq-1=numq) q-;q-; else if(numq+numpsum)q-; else p+; return res; 相關題目3Sum,1.83Sum,1.94Sum,1.103Sum Closest描
21、述Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.For example, given array S = -1 2 1 -4, and target = 1.The sum that is closest to the
22、 target is 2. (-1 + 2 + 1 = 2).分析在3Sum的基礎上改的代碼,夾逼的思想不變。最后return的值是三個數(shù)的和而不是差距ab的值,這里要特別注意。如果差距為零就直接返回,否則就一直夾逼到不能夾逼為止。比較值取target-n1代碼public class Solution public int threeSumClosest(int num, int target) Arrays.sort(num);/from small to big num int res=Integer.MAX_VALUE; int res_res=Integer.MAX_VALUE; f
23、or(int i=0;i0&numi=numi-1) continue; int n1=numi; int sum=target-n1;/near sum int p=i+1,q=num.length-1; int sub=Integer.MAX_VALUE; int res_sub=Integer.MAX_VALUE; while(psum) int ab=Math.abs(numq+nump+n1-target); if(subab)sub=ab; res_sub=numq+nump+n1; q-; elseint ab=Math.abs(numq+nump+n1-target);if(s
24、ubab) sub=ab; res_sub=numq+nump+n1; p+; if(ressub) res=sub;res_res=res_sub; return res_res; 相關題目3Sum,1.83Sum,1.94Sum,1.104Sum描述Given an array S of n integers, are there elements a,b,c, and d in S such that a+b+c+d = target?Find all unique quadruplets in the array which gives the sum of target.Note:
25、Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a b c d) The solution set must not contain duplicate quadruplets.For example, given array S = 1 0 -1 0 -2 2, and target = 0.A solution set is:(-1,0, 0, 1)(-2, -1, 1, 2)(-2,0, 0, 2)分析本題直接使用3Sum的思路,該3Sum的代碼使得能處理任意target值(3Sum原本只能
26、處理0值)。然后在外面加一層循環(huán)即可。代碼public class Solution public ListList threeSum(int num,int target,int start) ListList res=new ArrayListList(); if(num=null|num.length=0) return res; for(int i=start;inum.length;i+) ArrayList sub=new ArrayList(); if(istart&numi=numi-1) continue; int n1=numi; int sum=target-n1; in
27、t p=i+1,q=num.length-1; while(pq) if(numq+nump=sum) sub.add(n1); sub.add(nump); sub.add(numq); res.add(ArrayList)sub.clone(); sub=new ArrayList(); while(p+1p&numq-1=numq) q-;q-;else if(numq+numpsum)q-; else p+; return res; public ListList fourSum(int num, int target) ListList res=new ArrayListList()
28、; if(num=null|num.length=0) return res; Arrays.sort(num);/from small to big num for(int i=0;i0&numi=numi-1) continue; int tem=numi; ListList half=threeSum(num,target-tem,i+1); for(int j=0;jhalf.size();j+) half.get(j).add(0,tem); res.addAll(half); return res; 相關題目3Sum,1.83Sum,1.94Sum,1.10Remove Eleme
29、nt描述Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesnt matter what you leave beyond the new length.分析無代碼public class Solution public int removeElement(int A, int elem) if(A=null|A.length=0) return 0; int
30、p=0; for(int q=0;q=0;i-) if(numinumi+1) break; if(ii;j-) if(numjnumi) break; int tem=numi; numi=numj; numj=tem; Arrays.sort(num,i+1,num.length); 相關題目 Permutation Sequence, 見 2.1.13 Permutations, 見 8.3 Permutations II, 見 8.4 Combinations, 見 8.5Permutation Sequence描述The set 1,2,3,n contains a total of
31、 n! unique permutations.By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):123132213231312321Given n and k, return the kth permutation sequence.Note: Given n will be between 1 and 9 inclusive.分析兩種方法利用上題的next_permutation方法,但是這種超時了,因為每次都要排序一次,而我們只需要
32、弟k個,所以做了很多無用功。數(shù)學計算第i位(i從0開始)value=(k-1)/(n-i-1)!+1K=k-(numi-1)*(n-i-1)!Value從vis數(shù)組中查找,是第value個沒有訪問過的數(shù)。代碼一public class Solution public void nextPermutation(int num) if(num=null|num.length=0) return; /find the first small int i; for(i=num.length-2;i=0;i-) if(numinumi+1) break; if(ii;j-) if(numjnumi) b
33、reak; int tem=numi; numi=numj; numj=tem; Arrays.sort(num,i+1,num.length); public String getPermutation(int n, int k) / define the arrayint num=new intn;for(int i=0;in;i+)numi=i+1;while(k!=1)nextPermutation(num);k-;String res=;for(int i=0;inum.length;i+)res+=numi;return res;代碼二public class Solution p
34、ublic String getPermutation(int n, int k) String res=;int vis=new intn;/mark the visit statusint factorial=new intn;factorial0=1;for(int i=1;in;i+)factoriali=factoriali-1*(i+1);for(int i=0;in-1;i+)int value=(k-1)/factorialn-i-2+1;k=k-(value-1)*factorialn-i-2;int p=0;while(value!=0)if(visp%n=0)value-
35、;p+;p-;visp%n=1;res+=(p+1);for(int i=0;in;i+)if(visi=0)res+=(i+1);break;return res;Valid Sudoku描述Determine if a Sudoku is valid, according to: Sudoku Puzzles. The Rules.au/TheRules.aspx .The Sudoku board could be partially filled, where empty cells are filled with the character .A valid Sudoku board
36、 (partially filled) is not necessarily solvable. Only the filled cells need to be validated.分析 數(shù)獨的規(guī)則行不能重復,列不能重復,每個小方塊(3*3)不能重復。這里只需要判斷已填數(shù)的部分是否validate即可。代碼public class Solution public boolean isValidSudoku(char board) if(board=null|board.length=0) return false; int vis=new int10; for(int i=0;i9;i+)
37、vis=new int10; for(int j=0;j9;j+) if(!checkValide(vis,boardij) return false; /check col vis=new int10; for(int j=0;j9;j+) if(!checkValide(vis,boardji) return false; /check the block int x=0,y=0; for(int i=0;i9;i+) x=(i/3)*3;/line y=(i%3)*3;/col vis=new int10; for(int a=0;a3;a+) for(int b=0;b3;b+) if
38、(!checkValide(vis,boardx+ay+b) return false; return true; public boolean checkValide(int vis,char c)switch(c) case .:return true; default: int num=c-0; if(visnum=1) return false; else visnum=1; return true; 相關題目 Sudoku Solver, 見 10.10Trapping Rain Water描述Given n non-negative integers representing an
39、 elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given 0,1,0,2,1,0,1,3,2,1,2,1, return 6.分析有點動態(tài)規(guī)劃的思想,用pre和aft數(shù)組分別記錄前面的最大墻和后面的最大墻。這樣中間的面積等于min(prei,afti)-Ai,當然如果得數(shù)是負數(shù)則忽略。代碼public class Solution public int trap(int A) if(A=null|A.len
40、gth=0) return 0; int pre=new intA.length; int aft=new intA.length; for(int i=1;iprei-1) prei=Ai-1; else prei=prei-1; for(int i=A.length-2;i=0;i-) if(Ai+1afti+1) afti=Ai+1; else afti=afti+1; int res=0; for(int i=0;iA.length;i+) int min=preiafti?prei:afti; if(Aimin) res+=min-Ai; return res; 相關題目 Conta
41、iner With Most Water, 見 12.6 Largest Rectangle in Histogram, 見 4.1.3Rotate Image描述You are given an n n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place?分析首先想到,純模擬,從外到內(nèi)一圈一圈的轉,但這個方法太慢。如下圖,首先沿著對角線翻轉一次,然后沿著豎直中線翻轉一次。代碼public class Solution p
42、ublic void rotate(int matrix) if(matrix=null|matrix.length=0) return; int m=matrix.length; int n=matrix0.length; / matrixijmatrixji for(int i=0;im;i+) for(int j=0;jj) int tem=matrixij; matrixij=matrixji; matrixji=tem; /matrixijmatrixm-1-ij for(int i=0;in/2;i+) for(int j=0;j=0;i-) int tem=digitsi+ans
43、; digitsi=tem%10; ans=tem/10; if(ans=0) break; int res=null; int start=0; if(ans!=0) res=new intdigits.length+1; res0=1; start=1; else res=new intdigits.length; for(int i=0;idigits.length;i+) resi+start=digitsi; return res; Climbing Stairs描述You are climbing a stair case. It takes n steps to reach to
44、 the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?分析最基本的動態(tài)規(guī)劃問題,f(n)=f(n-1)+f(n-2),f(1)=1,f(2)=2;代碼public class Solution public int climbStairs(int n) if(n=2) return n; int array=new intn+1; array1=1; array2=2; for(int i=3;i=n;i+) arrayi=arrayi-1
45、+arrayi-2; return arrayn; 相關題目 Decode Ways, 見 13.10Gray Code描述The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin
46、 with 0.For example, given n = 2, return 0,1,3,2. Its gray code sequence is:00 - 001 - 111 - 310 - 2Note: For a given n, a gray code sequence is not uniquely defined. For example, 0,2,3,1 is also a valid gray code sequence according to the above definition. For now, the judge is able to judge based
47、on one instance of gray code sequence. Sorry about that.分析格雷碼的生成方法如下圖所示: 按照這種方式構造,十進制即每次加上ans*2,ans初始為2代碼public class Solution public List grayCode(int n) List res=new ArrayList(); if(n1) n-; ArrayList sub=new ArrayList(); for(int i=res.size()-1;i=0;i-) sub.add(res.get(i)+ans); ans*=2; res.addAll(su
48、b); return res; Set Matrix Zeroes描述Given a m n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m + n) space, but still not the best solut
49、ion.Could you devise a constant space solution?分析此題挺繁瑣的,我第一次做很多次都沒通過。代碼1:M+n的空間復雜度,可以通過測試代碼2:空間復雜度為1。不使用單獨的數(shù)組記錄行列的0情況,而是使用第一行和第一列分別記錄行列的情況。雖然代碼二寫法上復雜了些,但是思路其實跟代碼1是一樣的。代碼1public class Solution public void setZeroes(int matrix) if(matrix=null|matrix.length=0) return ; int m=matrix.length; int n=matrix
50、0.length; boolean lineflag=new booleanm; boolean colflag=new booleann; for(int i=0;im;i+) for(int j=0;jn;j+) if(matrixij=0) lineflagi=true; colflagj=true; for(int i=0;ilineflag.length;i+) if(lineflagi=true) setZero(true,i,matrix); for(int i=0;icolflag.length;i+) if(colflagi=true) setZero(false,i,mat
51、rix);public void setZero(boolean flag,int k,int matrix)/flag=true,line/flag=false,colif(flag=true)for(int j=0;jmatrix0.length;j+)matrixkj=0;elsefor(int i=0;imatrix.length;i+)matrixik=0;代碼2public class Solution public void setZeroes(int matrix) if(matrix=null|matrix.length=0) return ; int m=matrix.le
52、ngth; int n=matrix0.length; boolean first_line_flag=false; boolean first_col_flag=false; /check the first line for(int j=0;jn;j+) if(matrix0j=0) first_line_flag=true; break; /check the first col for(int i=0;im;i+) if(matrixi0=0) first_col_flag=true; break; for(int i=1;im;i+) for(int j=1;jn;j+) if(ma
53、trixij=0) matrix0j=0; matrixi0=0; for(int i=1;im;i+) if(matrixi0=0) setZero(true,i,matrix); for(int j=1;jn;j+) if(matrix0j=0) setZero(false,j,matrix); if(first_line_flag) setZero(true,0,matrix); if(first_col_flag) setZero(false,0,matrix); public void setZero(boolean flag,int k,int matrix)/flag=true,
54、line/flag=false,colif(flag=true)for(int j=0;jmatrix0.length;j+)matrixkj=0;elsefor(int i=0;imatrix.length;i+)matrixik=0;附錄資料:如何處理Java異常及常見異常六種異常處理的陋習你覺得自己是一個Java專家嗎?是否肯定自己已經(jīng)全面掌握了Java的異常處理機制?在下面這段代碼中,你能夠迅速找出異常處理的六個問題嗎? 1 OutputStreamWriter out = . 2 java.sql.Connection conn = . 3 try / 4 Statement sta
55、t = conn.createStatement(); 5 ResultSet rs = stat.executeQuery( 6 select uid, name from user); 7 while (rs.next() 8 9 out.println(ID: + rs.getString(uid) / 10 ,姓名: + rs.getString(name); 11 12 conn.close(); / 13 out.close(); 14 15 catch(Exception ex) / 16 17 ex.printStackTrace(); /, 18 作為一個Java程序員,你至
56、少應該能夠找出兩個問題。但是,如果你不能找出全部六個問題,請繼續(xù)閱讀本文。 本文討論的不是Java異常處理的一般性原則,因為這些原則已經(jīng)被大多數(shù)人熟知。我們要做的是分析各種可稱為“反例”(anti-pattern)的違背優(yōu)秀編碼規(guī)范的常見壞習慣,幫助讀者熟悉這些典型的反面例子,從而能夠在實際工作中敏銳地察覺和避免這些問題。 反例之一:丟棄異常 代碼:15行-18行。 這段代碼捕獲了異常卻不作任何處理,可以算得上Java編程中的殺手。從問題出現(xiàn)的頻繁程度和禍害程度來看,它也許可以和C/C+程序的一個惡名遠播的問題相提并論?不檢查緩沖區(qū)是否已滿。如果你看到了這種丟棄(而不是拋出)異常的情況,可以百
57、分之九十九地肯定代碼存在問題(在極少數(shù)情況下,這段代碼有存在的理由,但最好加上完整的注釋,以免引起別人誤解)。 這段代碼的錯誤在于,異常(幾乎)總是意味著某些事情不對勁了,或者說至少發(fā)生了某些不尋常的事情,我們不應該對程序發(fā)出的求救信號保持沉默和無動于衷。調(diào)用一下printStackTrace算不上“處理異常”。不錯,調(diào)用printStackTrace對調(diào)試程序有幫助,但程序調(diào)試階段結束之后, printStackTrace就不應再在異常處理模塊中擔負主要責任了。 丟棄異常的情形非常普遍。打開JDK的ThreadDeath類的文檔,可以看到下面這段說明:“特別地,雖然出現(xiàn)ThreadDeath
58、是一種正常的情形,但ThreadDeath類是Error而不是Exception的子類,因為許多應用會捕獲所有的Exception然后丟棄它不再理睬?!边@段話的意思是,雖然ThreadDeath代表的是一種普通的問題,但鑒于許多應用會試圖捕獲所有異常然后不予以適當?shù)奶幚?,所以JDK把 ThreadDeath定義成了Error的子類,因為Error類代表的是一般的應用不應該去捕獲的嚴重問題??梢?,丟棄異常這一壞習慣是如此常見,它甚至已經(jīng)影響到了Java本身的設計。 那么,應該怎樣改正呢?主要有四個選擇: 1、處理異常。針對該異常采取一些行動,例如修正問題、提醒某個人或進行其他一些處理,要根據(jù)具體
59、的情形確定應該采取的動作。再次說明,調(diào)用printStackTrace算不上已經(jīng)“處理好了異?!?。 2、重新拋出異常。處理異常的代碼在分析異常之后,認為自己不能處理它,重新拋出異常也不失為一種選擇。 3、把該異常轉換成另一種異常。大多數(shù)情況下,這是指把一個低級的異常轉換成應用級的異常(其含義更容易被用戶了解的異常)。 4、不要捕獲異常。 結論一:既然捕獲了異常,就要對它進行適當?shù)奶幚?。不要捕獲異常之后又把它丟棄,不予理睬。 反例之二:不指定具體的異常 代碼:15行。 許多時候人們會被這樣一種“美妙的”想法吸引:用一個catch語句捕獲所有的異常。最常見的情形就是使用catch(Exceptio
60、n ex)語句。但實際上,在絕大多數(shù)情況下,這種做法不值得提倡。為什么呢? 要理解其原因,我們必須回顧一下catch語句的用途。catch語句表示我們預期會出現(xiàn)某種異常,而且希望能夠處理該異常。異常類的作用就是告訴 Java編譯器我們想要處理的是哪一種異常。由于絕大多數(shù)異常都直接或間接從java.lang.Exception派生,catch (Exception ex)就相當于說我們想要處理幾乎所有的異常。 再來看看前面的代碼例子。我們真正想要捕獲的異常是什么呢?最明顯的一個是SQLException,這是JDBC操作中常見的異常。另一個可能的異常是IOException,因為它要操作Outp
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 建設工程招標標底的編制原則和依據(jù)學習情境二建設工程招標課件
- 全屋集成合同范本
- 2.1推動和平與發(fā)展課件- -2024-2025學年統(tǒng)編版道德與法治九年級下冊
- 鞏留縣2024-2025學年小升初??家族e數(shù)學檢測卷含解析
- 2025屆河北省唐山市古冶區(qū)小升初總復習數(shù)學測試卷含解析
- 2025屆山東省東營市利津縣重點中學小升初數(shù)學入學考試卷含解析
- 青海衛(wèi)生職業(yè)技術學院《數(shù)字音視頻制作》2023-2024學年第二學期期末試卷
- 嵩山少林武術職業(yè)學院《生物信息學與基因組學》2023-2024學年第二學期期末試卷
- 畢節(jié)職業(yè)技術學院《化工安全與環(huán)保A》2023-2024學年第二學期期末試卷
- 廣東藥科大學《中外文化概論》2023-2024學年第二學期期末試卷
- 2024解析:第五章透鏡及其應用-基礎練(解析版)
- 河南省第二屆職業(yè)技能大賽健康和社會照護項(世賽)項目技術工作文件
- 《護士禮儀與溝通》課件
- 專題05標點符號考點專訓(01)(含答案)2025年新高考語文一輪復習考點滿分寶典
- 保密法實施條例培訓
- 鉗工工藝學(第6版)完整全套教學課件
- DB11T 1035-2013 城市軌道交通能源消耗評價方法
- 老年科護士進修匯報
- 2024新能源光伏電站運行規(guī)程和檢修規(guī)程
- 同等學力英語申碩考試詞匯(第六版大綱)電子版
- 有機化學完整版本
評論
0/150
提交評論