java常用語句大全.doc_第1頁
java常用語句大全.doc_第2頁
java常用語句大全.doc_第3頁
java常用語句大全.doc_第4頁
java常用語句大全.doc_第5頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1. (根據(jù)老師講得內(nèi)容,自己總結(jié)的)2. Random r=new Random(); int i=r.nextInt(); 產(chǎn)生隨機(jī)數(shù)3. Scanner in = new Scanner(System.in);int n = in.nextInt(); 控制臺(tái)輸入4. System.out.println(Integer.toBinaryString(-12); 二進(jìn)制轉(zhuǎn)換 32位5. System.out.println(Long.toBinaryString(12); 二進(jìn)制轉(zhuǎn)換 64位6. System.out.println(Integer.toHexString(46); 十六進(jìn)制轉(zhuǎn)換7. Math.sqrt 開根號(hào)8. 條件? 條件成立:條件不成立 三目運(yùn)算9. switch(score/10)case 10:case 9:level=優(yōu)秀;break; switch.case 語句演示10. 循環(huán)條件(布爾表達(dá)式)循環(huán)體 while循環(huán)11. while(true) 和 for(;) 等價(jià) for(;)12. do 循環(huán)體1 while(循環(huán)條件2) 1-2-true-1-2-false-結(jié)束 do.while13. intary4=4,5,6; intary=new int4,5,6; 數(shù)組的三種寫法14. int ary=new intlength 15. System.out.println(Arrays.equals(arr1, arr2); /布爾類型 數(shù)組比較 16. Arrays.sort(balls); 數(shù)組排序17. /(原數(shù)組,原數(shù)組位置,目標(biāo)數(shù)組,目標(biāo)數(shù)組位置,個(gè)數(shù))18. System.arraycopy(ary1, 0, ary4, 0, ary1.length); 數(shù)組復(fù)制19. balls=Arrays.copyOf(balls,balls.length+1); 復(fù)制數(shù)組20. Cell cells = new Cell new Cell(0, 4), new Cell(0, 3),21. new Cell(0, 5), new Cell(1, 4), ; 二維數(shù)組22. Cellwall=new Cell2010; 23. Timer timer=new Timer();,24. timer.schedule(new MyTask(), 0,1000);/1000ms=1s 定時(shí)器25. 被執(zhí)行的任務(wù) 第一次執(zhí)行延時(shí)時(shí)間 時(shí)間間隔26. System.out.println(同一個(gè)對(duì)象: + (p1 = p2); 比較是否同一個(gè)對(duì)象27. System.out.println(內(nèi)容一樣: + (p1.equals(p2); 比較內(nèi)容是否一樣28. String lower=str.toLowerCase(); 將字符串轉(zhuǎn)換為小寫29. String upper=str.toUpperCase(); 將字符串轉(zhuǎn)換為大寫 30. String trim=str.trim(); 去掉字符串兩邊空白31. boolean starts=str.startsWith(Hel);boolean類型 是以Hel開頭 32. boolean ends = str.trim().endsWith(orld); 去掉空白,orld結(jié)尾33. int index = str.indexOf(in); 獲取字符位置34. index = str.indexOf(in,5); 從str第6個(gè)字符開始檢索第一次出現(xiàn)in的位置 35. int last = str.lastIndexOf(in); 從str中檢索最后一次出現(xiàn)in的位置 36. char chr = str.charAt(5); 獲取字符串中指定位置的字符37. String array=str.split(s); 以空格進(jìn)行拆分?jǐn)?shù)組38. int a=Integer.parseInt(array0); 將拆分的字符串轉(zhuǎn)換為數(shù)字39. byte data=str.getBytes(gbk); 以gbk(utf-8)進(jìn)行編碼40. String sub =str.substring(8,11); 截取字符串 切兩刀取中間 含頭不含尾41. String sub2 =str.substring(8); 重載方法 切一刀 取后面42. String leftPad=StringUtils.leftPad(hello,10,*); 43. 將給定的字符串左面補(bǔ)充padChar字符,直到字符串達(dá)到size長度為止 44. String rightPad=StringUtils.rightPad(hello,10,*)45. 將給定的字符串右面補(bǔ)充padChar字符,直到字符串達(dá)到size長度為止46. StringBuilder builder=new StringBuilder(str); 創(chuàng)建StringBuilder為了修改字符串 47. builder.append(!); 向字符末尾追加字符 48. String str1=builder.toString(); 獲取StringBuilder修改后的字符串49. builder.insert(offset , b) 參數(shù)1:指定位置字符串索引,從0開始 參數(shù)2:插入內(nèi)容 50. builder.replace(start,end,art); 替換字符串51. 參數(shù)1:被替換位置的起始位置 參數(shù)2:被替換位置的結(jié)束位置 參數(shù)3:替換內(nèi)容 52. builder.delete(start,end); 參數(shù)1:要?jiǎng)h的起始位置 參數(shù)2:要?jiǎng)h的結(jié)束位置53. builder.reverse(); 字符串反轉(zhuǎn)54. Date date=new Date(); 創(chuàng)建一個(gè)用于描述當(dāng)前系統(tǒng)時(shí)間的Date對(duì)象55. long now=System.currentTimeMillis(); - 獲取當(dāng)前系統(tǒng)時(shí)間毫秒值 56. now +=1000*60*60*24;date.setTime(now); 設(shè)定一個(gè)毫秒值,使用data表示這個(gè)時(shí)間57. SimpleDateFormat format=new SimpleDateFormat(yyyy-MM-dd); 58. 創(chuàng)建SimpleDateFormat實(shí)例時(shí),我們需要傳入一個(gè)字符串,這個(gè)字符串是用來描述轉(zhuǎn)換的格式 59. String nowStr=format.format(now);/ 將Date轉(zhuǎn)換為字符串60. Date date=format2.parse(dateStr);/ 將字符串轉(zhuǎn)換為時(shí)間格式61. Calendar calendar=Calendar.getInstance(); 默認(rèn)創(chuàng)建的日歷類表示的日期為當(dāng)前系統(tǒng)時(shí)間62. Date date=calendar.getTime();/ 將Calendar轉(zhuǎn)換為date63. int year=calendar.get(Calendar.YEAR); 獲取calendar的某個(gè)時(shí)間單位上的值64. int max=now.getActualMaximum(Calendar.DAY_OF_YEAR);獲取Calendar表示的日期所處年的最大天數(shù)65. calendar.set(Calendar.MONTH, 11); 設(shè)置日期類使之描述66. int a=Intrger.parseInt(123123); 將字符串轉(zhuǎn)換成對(duì)應(yīng)的基本類型67. Integer a=Integer.valueOf(1); 包裝類對(duì)字面量的優(yōu)化68. int a=Integer.parseInt(123); 將字符串轉(zhuǎn)換為基本類型數(shù)據(jù)69. String bStr=Integer.toBinaryString(100);Integer70. 提供了可以將數(shù)字化轉(zhuǎn)為2進(jìn)制和16進(jìn)制并用字符串去描述71. String hStr=Integer.toHexString(100);72. int max=Integer.MAX_VALUE;double dMax=Integer.MAX_VALUE;73. int min=Integer.MIN_VALUE; 包裝類常用的常量 最大值 和 最小值74. BigDecimal num1 = new BigDecimal(3.0); 用于計(jì)算更精確的小數(shù)75. BigDecimal num4 = num1.divide(num2,8,BigDecimal.ROUND_HALF_UP); 四舍五入保留8位76. BigInteger num1=new BigInteger(123); 用于計(jì)算更長的整數(shù)77. add 加法 subtract 減法 multiply 乘法 divide 除法78. List list=new ArrayList(); 創(chuàng)建集合79. list.add(一); 添加元素80. list.clear(); 清空集合元素81. System.out.println(list.isEmpty(); 判斷是否為空82. list.remove(p); 刪除元素83. list1.addAll(list2); 將集合2中的元素放入集合1中84. list1.removeAll(list3); 刪除相同的元素(equals比較)85. list1.retainAll(list2); 只保留都有的元素86. list.contains(p); p在集合中么87. Object old=list.set(1, 二); 用于替換集合中指定位置上的元素,返回值為被替換的元素88. list.add(2,二); 向集合的指定位置插入元素89. Object old=list.remove(1); 刪除指定位置元素 返回值為被刪除元素90. list.indexOf(three) 查詢集合中元素的位置 (equals比較)91. list.lastIndexOf(three) 查詢集合中的元素最后出現(xiàn)的位置(如果沒有結(jié)果為-1)92. String array=(String)list.toArray(new String0); 用于將集合轉(zhuǎn)換為數(shù)組93. 我們給定的數(shù)組實(shí)例不需要長度94. Iterator it=list.iterator(); 迭代器是專門為while循環(huán)設(shè)計(jì)的95. while(it.hasNext()String element=(String)it.next(); next方法獲取集合中下一個(gè)元素 96. if(#.equals(element)it.remove(); 將上面通過next方法獲取的元素從集合中刪除97. List list = new ArrayList(); 創(chuàng)建集合 有泛型 98. for (int i : array) 新循環(huán) 99. List subList=list.subList(3, 8); 獲取子集 含頭不含尾100. for(int i=0;isubList.size();i+)101. subList.set(i,subList.get(i)*10); 集合元素*10102. Queue queue=new LinkedList(); 創(chuàng)建隊(duì)列(先進(jìn)現(xiàn)出)103. queue.offer(A); 向隊(duì)列里添加元素104. System.out.println(queue.poll(); 獲取隊(duì)首元素并刪除105. System.out.println(queue.peek(); 獲取隊(duì)首元素不刪除106. Deque deque=new LinkedList(); 創(chuàng)建一個(gè)棧先進(jìn)后出)107. deque.push(A); 向棧里添加元素(108. while(deque.peek()!=null)String element=deque.pop();System.out.println(element);109. 獲取隊(duì)首元素不刪除 (判斷是否有元素) 獲取隊(duì)首元素并刪除 110. Collections.sort(list); 使用集合的工具類對(duì)集合元素進(jìn)行自然排序 由小至大111. Comparator comparator=new Comparator() 自定義排序112. public int compare(String o1, String o2) 113. return o1.length()-o2.length(); 字母多少排序114. Collections.sort(list,comparator); 調(diào)用排序方法115. Set set=new HashSet(); set集合 不重復(fù)集116. set.add(1); 添加元素117. Iterator it=set.iterator(); set集合只能用迭代器遍歷集合118. while(it.hasNext()String str=it.next();System.out.println(str);119. Map map = new HashMap(); Map要求的泛型有兩個(gè)泛型120. map.put(name,hoss); 添加元素121. String name=map.get(name); 獲取數(shù)據(jù)122. map.containsKey(name) boolean類型 判斷集合中key是否有name 123. map.containsValue(name) boolean類型 判斷集合中value是否有name124. Set KeySet=map.keySet(); 獲取Map中的所有key 值泛型應(yīng)一致125. Map map = new LinkedHashMap(); 可以保證存放順序一致126. SetEntry entrySet=map.entrySet(); 遍歷Map的健值對(duì) 127. Collection ValueSet=map.values();/返回值是集合 獲取Map中的所有value128. try出現(xiàn)錯(cuò)誤的代碼片段catch(異常的名字)解決異常 捕獲異常 catch可以寫多個(gè)129. e.printStackTrace(); 輸出錯(cuò)誤堆棧信息,用于幫助我們得知錯(cuò)誤出現(xiàn)的位置130. e.getMessage() 獲取錯(cuò)誤信息,通常是得知錯(cuò)誤原因131. throws ParseException 拋出異常132. NullPointerException e 出現(xiàn)空指針異常133. ArrayIndexOutOfBoundsException e 出現(xiàn)了數(shù)組越界134. NumberFormatException e 出現(xiàn)數(shù)字格式異常135. RuntimeException e 非檢測(cè)異常136. System.out.println(fileName:+file.getName(); 獲取文件或目錄的名字137. System.out.println(length:+file.length(); 文件大小 以字節(jié)為單位138. System.out.println(lastModified:+file.lastModified(); 文件最后修改時(shí)間 毫秒值139. System.out.println(path:+file.getPath(); 相對(duì)路徑140. System.out.println(abs_path+file.getAbsolutePath(); 絕對(duì)路徑141. System.out.println(abs_path2+file.getCanonicalPath();獲取系統(tǒng)標(biāo)準(zhǔn)的絕對(duì)路徑需要捕獲異常142. System.out.println(是否存在:+file.exists(); 判斷文件是否存在143. System.out.println(是否是文件:+file.isFile(); 判斷是否是文件144. System.out.println(是否是目錄:+file.isDirectory(); 斷是否為一個(gè)目錄145. System.out.println(是否可讀:+file.canRead(); 判斷是否可讀146. System.out.println(是否可寫:+file.canWrite(); 判斷是否可寫147. fileName.endsWith(.txt); 是否以.txt結(jié)尾148. File.separator (/) 149. dir.mkdir(); 創(chuàng)建目錄 150. file.createNewFile(); 創(chuàng)建文件151. dir.mkdirs(); 創(chuàng)建多級(jí)目錄152. File subs = file.listFiles(); 獲取子項(xiàng),以數(shù)組形式返回153. RandomAccessFile raf =new RandomAccessFile(file,rw);154. 參數(shù)1:要進(jìn)行讀寫操作的文件對(duì)象 參數(shù)2:讀寫模式 rw讀寫 r只讀155. raf.writeInt(a); 寫基本類型的數(shù)據(jù)方法4個(gè)字節(jié)156. i = raf.readInt(); 讀4個(gè)字節(jié) int類型 157. raf.seek(0); 游標(biāo)移動(dòng)到文件開始后在進(jìn)行讀取數(shù)據(jù)158. System.out.println(raf.getFilePointer(); 輸出游標(biāo)位置159. FileOutputStream fis = new FileOutputStream(fos.dat);160. 打開文件指定對(duì)象的輸出流,進(jìn)行寫出操作 161. FileOutputStream fis = new FileOutputStream(fos.dat,true);162. 打開文件指定對(duì)象的輸出流,并在文件末尾進(jìn)行寫操作163. FileOutputStream fos = new FileOutputStream(jvm_copy.bmp); 164. BufferedInputStream bis = new BufferedInputStream(fis); (以字節(jié))高級(jí)流,增加讀寫效率165. BufferedOutputStream bos = new BufferedOutputStream(fos); 必須在低級(jí)流之下 166. 高級(jí)流可以疊加167. DataOutputStream dos = new DataOutputStream(fos); 基于基本數(shù)據(jù)的高級(jí)流 寫出本程序168. DataInputStream dis = new DataInputStream(fis); 基于基本數(shù)據(jù)的高級(jí)流 讀入本程序169. OutputStreamWriter writer = new OutputStreamWriter(fos,GBK);用于寫字符的字符輸出流170. InputStreamReader read = new InputStreamReader(fis); (高級(jí)流) 用于讀字符的字符輸入流171. BufferedReader br = new BufferedReader(reader); 按行讀取字符串的字符輸入流172. PringWriter pw = new PrintWrier() 將字節(jié)輸出流變?yōu)榫彌_字符輸出流173. br.newLine(); 換行174. InputStream in = System.in; 獲取到了鍵盤

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論