<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## Java API API(Application Programming Interface,應用程序編程接口)是一些預先定義的函數,通俗一點就是別人已經寫好的方法,我們直接進行調用就可以使用響應的功能。 ## Java常用API #### String類 ```java public class Main { public static void main(String[] args) { // 1 length()字符串的長度 String a = "Hello Word!"; System.out.println(a.length()); // 輸出的結果是字符串長度10。 // 2 charAt()截取一個字符 String a = "Hello Word"; System.out.println(a.charAt(1)); // 輸出的結果是字符串a的下標為1的字符e。 // 3 getchars()截取多個字符并由其他字符串接收 String a = "Hello Word"; char[] b = new char[10]; a.getChars(0, 5, b, 0); System.out.println(b); // 輸出的結果為Hello,其中第一個參數0是要截取的字符串的初始下標(int sourceStart),第二個參數5是要截取的字符串的結束后的下一個下標(int sourceEnd)也就是實際截取到的下標是int sourceEnd-1,第三個參數是接收的字符串(char target[]),最后一個參數是接收的字符串開始接收的位置。 // 4 getBytes()將字符串變成一個byte數組 String a = "Hello Word"; byte b[] = a.getBytes(); System.out.println(new String(b)); // 輸出的結果為Hello Word的byte數組。 // 5 toCharArray()將字符串變成一個字符數組 String a = "Hello Word"; char[]b = a.toCharArray(); System.out.println(b); // 輸出的結果為Hello Word字符數組。 // 6 equals()和equalsIgnoreCase()比較兩個字符串是否相等,前者區分大小寫,后者不區分 String a = "Hello Word"; String b = "hello word"; System.out.println(a.equals(b)); System.out.println(a.equalsIgnoreCase(b)); // 輸出的結果為第一條為false,第二條為true。 // 7 startsWith()和endsWith()判斷字符串是不是以特定的字符開頭或結束 String a = "Hello Word"; System.out.println(a.startsWith("ee")); System.out.println(a.endsWith("rd")); // 輸出的結果第一條為false,第二條為true。 // 8 toUpperCase()和toLowerCase()將字符串轉換為大寫或小寫 String a = "Hello Word"; System.out.println(a.toUpperCase()); System.out.println(a.toLowerCase()); // 輸出的結果第一條為“HELLO WORD”,第二條為“hello word”。 // 9 concat() 連接兩個字符串 String a = "Hello Word"; String b = "你好"; System.out.println(b.concat(a)); // 輸出的結果為“你好Hello Word”。 // 10 trim()去掉起始和結束的空格 String a = " Hello Word "; System.out.println(a.trim()); // 輸出的結果為“Hello Word”。 // 11 substring()截取字符串 String a = "Hello Word"; System.out.println(a.substring(0, 5)); System.out.println(a.substring(6)); // 輸出的結果第一條為“Hello”,第一個參數0(beginIndex)是開始截取的位置,第二個參數5(endIndex)是截取結束的位置,輸出的結果第二條是“Word”,參數6(beginIndex)是開始截取的位置。 // 12 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出現的地方,后者是查找字符或字符串最后一次出現的地方 String a = "Hello Word"; System.out.println(a.indexOf("o")); System.out.println(a.lastIndexOf("o")); // 輸出的結果第一條是4,是o第一次出現的下標,第二條是7,是o最后一次出現的下標。 // 13 compareTo()和compareToIgnoreCase()按字典順序比較兩個字符串的大小,前者區分大小寫,后者不區分 String a = "Hello Word"; String b = "hello word"; System.out.println(a.compareTo(b)); System.out.println(a.compareToIgnoreCase(b)); // 輸出的結果第一條為-32,第二條為0,兩個字符串在字典順序中大小相同,返回0。 // 14 replace() 替換 String a = "Hello Word"; String b = "你好"; System.out.println(a.replace(a, b)); System.out.println(a.replace(a, "HELLO WORD")); System.out.println(b.replace("你", "大家")); // 輸出的結果第一條為“你好”,第二條為“HELLO WORD”,第三條為“大家好”。 } } ``` #### StringBuffer類 ```java public class Main { public static void main(String[] args) { // 在sb尾部追加一個字符串 StringBuffer sb = new StringBuffer("Hello "); sb.append("world"); System.out.println(sb.toString()); // 返回下標為1的字符 StringBuffer sb = new StringBuffer("Hello "); sb.charAt(1); System.out.println(sb.toString()); // 在 1 處插入新的字符串 d StringBuffer sb = new StringBuffer("Hello "); sb.insert(1, "d"); System.out.println(sb.toString()); // 反轉字符 StringBuffer sb = new StringBuffer("Hello "); sb.reverse(); System.out.println(sb.toString()); // 刪除字符串 StringBuffer sb = new StringBuffer("Hello "); sb.delete(1, 2); System.out.println(sb.toString()); // 替換字符串 從 3開始到4結束 StringBuffer sb = new StringBuffer("Hello "); sb.replace(3, 4, "new"); System.out.println(sb.toString()); } } ``` #### Math類 ```java public class Main { public static void main(String[] args) { /** * abs求絕對值 */ System.out.println(Math.abs(-10.4)); //10.4 System.out.println(Math.abs(10.1)); //10.1 /** * ceil天花板的意思,就是返回大的值,注意一些特殊值 */ System.out.println(Math.ceil(-10.1)); //-10.0 System.out.println(Math.ceil(10.7)); //11.0 System.out.println(Math.ceil(-0.7)); //-0.0 System.out.println(Math.ceil(0.0)); //0.0 System.out.println(Math.ceil(-0.0)); //-0.0 System.out.println("my name is -0.5 " + Math.ceil(-0.5)); /** * floor地板的意思,就是返回小的值 */ System.out.println(Math.floor(-10.1)); //-11.0 System.out.println(Math.floor(10.7)); //10.0 System.out.println(Math.floor(-0.7)); //-1.0 System.out.println(Math.floor(0.0)); //0.0 System.out.println(Math.floor(-0.0)); //-0.0 /** * max 兩個中返回大的值,min和它相反,就不舉例了 */ System.out.println(Math.max(-10.1, -10)); //-10.0 System.out.println(Math.max(10.7, 10)); //10.7 System.out.println(Math.max(0.0, -0.0)); //0.0 /** * random 取得一個大于或者等于0.0小于不等于1.0的隨機數 */ System.out.println(Math.random()); //0.08417657924317234 System.out.println(Math.random()); //0.43527904004403717 /** * rint 四舍五入,返回double值 * 注意.5的時候會取偶數 */ System.out.println(Math.rint(10.1)); //10.0 System.out.println(Math.rint(10.7)); //11.0 System.out.println(Math.rint(11.5)); //12.0 System.out.println(Math.rint(10.5)); //10.0 System.out.println(Math.rint(10.51)); //11.0 System.out.println(Math.rint(-10.5)); //-10.0 System.out.println(Math.rint(-11.5)); //-12.0 System.out.println(Math.rint(-10.51)); //-11.0 System.out.println(Math.rint(-10.6)); //-11.0 System.out.println(Math.rint(-10.2)); //-10.0 /** * round 四舍五入,float時返回int值,double時返回long值 */ System.out.println(Math.round(10.1)); //10 System.out.println(Math.round(10.7)); //11 System.out.println(Math.round(10.5)); //11 System.out.println(Math.round(10.51)); //11 System.out.println(Math.round(-10.5)); //-10 System.out.println(Math.round(-10.51)); //-11 System.out.println(Math.round(-10.6)); //-11 System.out.println(Math.round(-10.2)); //-10 } } ``` #### Random類 ```java import java.util.Random; public class Main { public static void main(String[] args) { Random rnd = new Random(); String[] str = {"紅桃1", "紅桃2", "紅桃3", "紅桃4", "紅桃5", "紅桃6"}; for (int i = 0; i < 5; i++) { int m = rnd.nextInt(6); System.out.println(str[m]); } /** * 紅桃2 * 紅桃3 * 紅桃2 * 紅桃1 * 紅桃1 */ } } ``` #### Date類 ```java import java.util.Date; public class Main { public static void main(String[] args) { Date date = new Date(); System.out.println("當前時間:" + date); } /** * 當前時間:Tue Dec 11 10:11:49 CST 2018 */ } ``` #### SimpleDateFormat類 ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { /** * 將日期對象格式為指定格式的日期字符串 * * @return */ public static String formatDate(Date date, String format) { String result = ""; SimpleDateFormat sm = new SimpleDateFormat(format); if (date != null) { result = sm.format(date); } return result; } /** * 將日期字符串轉換成日期對象 * * @return * @throws ParseException */ public static Date formatToDate(String dateStr, String format) throws ParseException { SimpleDateFormat sm = new SimpleDateFormat(format); return sm.parse(dateStr); } public static void main(String[] args) throws ParseException { Date date = new Date(); SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sm.format(date)); // 下面是調用封裝方法實現 System.out.println(formatDate(date, "yyyy-MM-dd")); String date1 = "2016-10-22 18:18:35"; Date da = formatToDate(date1, "yyyy-MM-dd"); // 將日期字符串轉換成日期對象 System.out.println(formatDate(da, "yyyy-MM-dd")); // 將日期對象格式為指定格式的日期字符串 System.out.println(formatDate(da, "yyyy-MM-dd HH:mm:ss")); } } ``` #### List接口、ArrayList集合 ```java import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); // 使用List中的添加方法 add(int index,Object Obj) // 添加的角標 就是后面元素 所在的位置 不能越界 list.add(4, "e"); System.out.println(list); // 輸出的是 a, b, c, d, e // set方法(不要越界) list.set(4, "z"); System.out.println(list); // 通過角標獲取對應的元素 Object object = list.get(3); System.out.println(object); // 通過get方法 進行遍歷 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } // 刪除(根據角標刪除) 返回的是 被刪除的元素 Object remove = list.remove(4); System.out.println(remove); System.out.println(list); } } ``` #### Map接口、HashMap集合 ```java import java.util.*; public class Main { public static void main(String[] args) { Map map = new HashMap(); map.put("發發發", "8"); map.put("還挺好聽", "31"); map.put("替換", "12"); map.put("聚", "14"); System.out.print(map); } /** * 結果:{還挺好聽=31, 發發發=8, 聚=14, 替換=12} */ } ``` ## 包裝類 在Java中,很多類的方法都需要接收引用類型的對象,此時就無法將一個基本數據類型的值傳入。為了解決這樣的問題,JDK 中提供了一系列的包裝類,通過這些包裝類可以將基本數據類型的值包裝為引用數據類型的對象。在Java中,每種基本類型都有對應的包裝類。 基本數據類型對應的包裝類 | 基本數據類型 | 對應的包裝類 | | ------------ | ------------ | | byte | Byte | | char | Character | | int | Integer | | short | Short | | long | Long | | float | Float | | double | Double | | boolean | Boolean | 包裝類和基本數據類型在進行轉換時,引入了裝箱和拆箱的概念,其中裝箱是指將基本數據類型的值轉為引用數據類型;反之,拆箱是指將引用數據類型的對象轉為基本數據類型。 ## JDK7新特性switch支持字符串 在JDK7中,switch語句的判斷條件增加了對字符串類型的支持。由于字符串的操作在編程中使用頻繁,這個新特性的出現為Java編程帶來了便利。接下來通過一個案例演示一下在switch語句中使用字符串進行匹配。 ```java public class Example { public static void main(String[] args) { String week = "Friday"; switch (week) { case "Monday": System.out.println("星期一"); break; case "Tuesday": System.out.println("星期二"); break; case "Wednesday": System.out.println("星期三"); break; case "Thursday": System.out.println("星期四"); break; case "Friday": System.out.println("星期五"); break; case "Saturday": System.out.println("星期六"); break; case "Sunday": System.out.println("星期天"); break; default: System.out.println("你的輸入不正確..."); } } } ``` 運行結果: ``` 星期五 ``` ## Java幫助文檔 java幫助文檔就是java的詞典,就像漢語和漢語詞典的關系一樣。 Java1.8 幫助文檔 中文 – 谷歌版在線版: https://blog.fondme.cn/apidoc/jdk-1.8-google/ 中文 – 有道版在線版: https://blog.fondme.cn/apidoc/jdk-1.8-youdao/ 中文 – 百度版在線版: https://blog.fondme.cn/apidoc/jdk-1.8-baidu/
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看