Java基礎(chǔ)語法_Day13

一、API概述
  • API概念

API(Application Programming Interface) : 應(yīng)用程序編程接口
編寫一個機(jī)器人程序去控制機(jī)器人踢足球,程序就需要向機(jī)器人發(fā)出向前跑、向后跑、射門、搶球等各種命令,沒有編過程序的人很難想象這樣的程序如何編寫。但是對于有經(jīng)驗(yàn)的開發(fā)人員來說,知道機(jī)器人廠商一定會提供一些用于控制機(jī)器人的Java類,這些類中定義好了操作機(jī)器人各種動作的方法。其實(shí),這些Java類就是機(jī)器人廠商提供給應(yīng)用程序編程的接口,大家把這些類稱為API。本章涉及的Java API指的就是JDK中提供的各種功能的Java類。

  • 快速使用API步驟:

A:打開幫助文檔
B:點(diǎn)擊顯示,找到索引,看到輸入框
C:你要學(xué)習(xí)什么內(nèi)容,你就在框框里面輸入什么內(nèi)容
舉例:Random
D:看包
java.lang包下的類在使用的時候是不需要導(dǎo)包的
E:看類的描述
Random類是用于生成隨機(jī)數(shù)的類
F:看構(gòu)造方法
Random():無參構(gòu)造方法
Random r = new Random();
G:看成員方法
public int nextInt(int n):產(chǎn)生的是一個[0,n)范圍內(nèi)的隨機(jī)數(shù)
調(diào)用方法:
看返回值類型:人家返回什么類型,你就用什么類型接收
看方法名:名字不要寫錯了
看形式參數(shù):人家要幾個參數(shù),你就給幾個,人家要什么數(shù)據(jù)類型的,你就給什么數(shù)據(jù)類型的
int number = r.nextInt(100);

二、Scanner類 與 String類
  • Scanner類
  • Scanner類作用
    用Scanner類的方法可以完成接收鍵盤錄入的數(shù)據(jù)
  • Scanner類接受鍵盤錄入的字符串
  • 案例代碼一:
package com.neuedu.demo;
import java.util.Scanner;
/*
* Scanner:用于獲取鍵盤錄入的數(shù)據(jù)。(基本數(shù)據(jù)類型,字符串?dāng)?shù)據(jù))
* public String nextLine():獲取鍵盤錄入的字符串?dāng)?shù)據(jù)
*/
public class ScannerDemo {
  public static void main(String[] args) {
      //創(chuàng)建鍵盤錄入對象
      Scanner sc = new Scanner(System.in);
      //接收數(shù)據(jù)
      System.out.println("請輸入一個字符串?dāng)?shù)據(jù):");
      String s = sc.nextLine();
      
      //輸出結(jié)果
      System.out.println("s:"+s);
  }
}
  • String類
  • String類概述
    通過JDK提供的API,查看String類的說明
    A:"abc"是String類的一個實(shí)例,或者成為String類的一個對象
    B:字符串字面值"abc"也可以看成是一個字符串對象
    C:字符串是常量,一旦被賦值,就不能被改變
    D:字符串本質(zhì)是一個字符數(shù)組
  • String類的構(gòu)造方法
    String(String original):把字符串?dāng)?shù)據(jù)封裝成字符串對象
    String(char[] value):把字符數(shù)組的數(shù)據(jù)封裝成字符串對象
    String(char[] value, int index, int count):把字符數(shù)組中的一部分?jǐn)?shù)據(jù)封裝成字符串對象
  • 常用構(gòu)造方法演示
  • 案例代碼二:
package com.neuedu.demo;
/*
* String:字符串類
*         由多個字符組成的一串?dāng)?shù)據(jù)
*         字符串其本質(zhì)是一個字符數(shù)組
* 
* 構(gòu)造方法:
*         String(String original):把字符串?dāng)?shù)據(jù)封裝成字符串對象
*         String(char[] value):把字符數(shù)組的數(shù)據(jù)封裝成字符串對象
*         String(char[] value, int index, int count):把字符數(shù)組中的一部分?jǐn)?shù)據(jù)封裝成字符串對象
* 
* 注意:字符串是一種比較特殊的引用數(shù)據(jù)類型,直接輸出字符串對象輸出的是該對象中的數(shù)據(jù)。
*/
public class StringDemo {
  public static void main(String[] args) {
      //方式1
      //String(String original):把字符串?dāng)?shù)據(jù)封裝成字符串對象
      String s1 = new String("hello");
      System.out.println("s1:"+s1);
      System.out.println("---------");
      
      //方式2
      //String(char[] value):把字符數(shù)組的數(shù)據(jù)封裝成字符串對象
      char[] chs = {'h','e','l','l','o'};
      String s2 = new String(chs);
      System.out.println("s2:"+s2);
      System.out.println("---------");
      
      //方式3
      //String(char[] value, int index, int count):把字符數(shù)組中的一部分?jǐn)?shù)據(jù)封裝成字符串對象
      //String s3 = new String(chs,0,chs.length);
      String s3 = new String(chs,1,3);
      System.out.println("s3:"+s3);
      System.out.println("---------");
      
      //方式4
      String s4 = "hello";
      System.out.println("s4:"+s4);
  }
}
  • 創(chuàng)建字符串對象兩種方式的區(qū)別
  • 案例代碼三:


    內(nèi)存圖.png
package com.neuedu.demo;
/*
* 通過構(gòu)造方法創(chuàng)建的字符串對象和直接賦值方式創(chuàng)建的字符串對象有什么區(qū)別呢?
*         通過構(gòu)造方法創(chuàng)建字符串對象是在堆內(nèi)存。
*         直接賦值方式創(chuàng)建對象是在方法區(qū)的常量池。
*         
* ==:
*         基本數(shù)據(jù)類型:比較的是基本數(shù)據(jù)類型的值是否相同
*         引用數(shù)據(jù)類型:比較的是引用數(shù)據(jù)類型的地址值是否相同
*/
public class StringDemo2 {
  public static void main(String[] args) {
      String s1 = new String("hello");
      String s2 = "hello";
      
      System.out.println("s1:"+s1);
      System.out.println("s2:"+s2);
      
      System.out.println("s1==s2:"+(s1==s2)); //false
      
      String s3 = "hello";
      System.out.println("s1==s3:"+(s1==s3)); //false
      System.out.println("s2==s3:"+(s2==s3)); //true
  }
}
  • String類的判斷功能
    boolean equals(Object obj):比較字符串的內(nèi)容是否相同
    boolean equalsIgnoreCase(String str):比較字符串的內(nèi)容是否相同,忽略大小寫
    boolean startsWith(String str):判斷字符串對象是否以指定的str開頭
    boolean endsWith(String str):判斷字符串對象是否以指定的str結(jié)尾
  • 判斷方法演示
  • 案例代碼四:
package com.neuedu.demo;
/*
* Object:是類層次結(jié)構(gòu)中的根類,所有的類都直接或者間接的繼承自該類。
* 如果一個方法的形式參數(shù)是Object,那么這里我們就可以傳遞它的任意的子類對象。
* 
* String類的判斷功能:
* boolean equals(Object obj):比較字符串的內(nèi)容是否相同
* boolean equalsIgnoreCase(String str):比較字符串的內(nèi)容是否相同,忽略大小寫
* boolean startsWith(String str):判斷字符串對象是否以指定的str開頭
* boolean endsWith(String str):判斷字符串對象是否以指定的str結(jié)尾
*/
public class StringDemo {
  public static void main(String[] args) {
      //創(chuàng)建字符串對象
      String s1 = "hello";
      String s2 = "hello";
      String s3 = "Hello";
      
      //boolean equals(Object obj):比較字符串的內(nèi)容是否相同
      System.out.println(s1.equals(s2));
      System.out.println(s1.equals(s3));
      System.out.println("-----------");
      
      //boolean equalsIgnoreCase(String str):比較字符串的內(nèi)容是否相同,忽略大小寫
      System.out.println(s1.equalsIgnoreCase(s2));
      System.out.println(s1.equalsIgnoreCase(s3));
      System.out.println("-----------");
      
      //boolean startsWith(String str):判斷字符串對象是否以指定的str開頭
      System.out.println(s1.startsWith("he"));
      System.out.println(s1.startsWith("ll"));
  }
}
  • 判斷功能案例
    需求:模擬登錄,給三次機(jī)會,并提示還有幾次
  • 案例代碼五:
package com.neuedu.demo;
import java.util.Scanner;
/*
* 模擬登錄,給三次機(jī)會,并提示還有幾次。
* 
* 分析:
*         A:定義兩個字符串對象,用于存儲已經(jīng)存在的用戶名和密碼
*         B:鍵盤錄入用戶名和密碼
*         C:拿鍵盤錄入的用戶名和密碼和已經(jīng)存在的用戶名和密碼進(jìn)行比較
*             如果內(nèi)容相同,提示登錄成功
*             如果內(nèi)容不同,提示登錄失敗,并提示還有幾次機(jī)會
*/
public class StringTest {
  public static void main(String[] args) {
      //定義兩個字符串對象,用于存儲已經(jīng)存在的用戶名和密碼
      String username = "admin";
      String password = "admin";
      
      //給三次機(jī)會,用for循環(huán)實(shí)現(xiàn)
      for(int x=0; x<3; x++) {
          //鍵盤錄入用戶名和密碼
          Scanner sc = new Scanner(System.in);
          System.out.println("請輸入用戶名:");
          String name = sc.nextLine();
          System.out.println("請輸入密碼:");
          String pwd = sc.nextLine();
          
          //拿鍵盤錄入的用戶名和密碼和已經(jīng)存在的用戶名和密碼進(jìn)行比較
          if(username.equals(name) && password.equals(pwd)) {
              System.out.println("登錄成功");
              break;
          }else {
              if((2-x) ==0) {
                  System.out.println("用戶名和密碼被鎖定,請與管理員聯(lián)系");
              }else {
                  System.out.println("登錄失敗,你還有"+(2-x)+"次機(jī)會"); //2,1,0
              }
          }
      }
  }
}
  • String類的獲取功能
  • 獲取方法演示
package com.neuedu.demo;
/*
* String類的獲取功能:
* int length():獲取字符串的長度,其實(shí)也就是字符個數(shù)
* char charAt(int index):獲取指定索引處的字符
* int indexOf(String str):獲取str在字符串對象中第一次出現(xiàn)的索引
* String substring(int start):從start開始截取字符串
* String substring(int start,int end):從start開始,到end結(jié)束截取字符串。包括start,不包括end
*/
public class StringDemo {
  public static void main(String[] args) {
      //創(chuàng)建字符串對象
      String s = "helloworld";
      
      //int length():獲取字符串的長度,其實(shí)也就是字符個數(shù)
      System.out.println(s.length());
      System.out.println("--------");
      
      //char charAt(int index):獲取指定索引處的字符
      System.out.println(s.charAt(0));
      System.out.println(s.charAt(1));
      System.out.println("--------");
      
      //int indexOf(String str):獲取str在字符串對象中第一次出現(xiàn)的索引
      System.out.println(s.indexOf("l"));
      System.out.println(s.indexOf("owo"));
      System.out.println(s.indexOf("ak"));
      System.out.println("--------");
      
      //String substring(int start):從start開始截取字符串
      System.out.println(s.substring(0));
      System.out.println(s.substring(5));
      System.out.println("--------");
      
      //String substring(int start,int end):從start開始,到end結(jié)束截取字符串
      System.out.println(s.substring(0, s.length()));
      System.out.println(s.substring(3,8));
  }
}
  • 獲取功能案例
  • 案例代碼六:
package com.neuedu.demo
/*
* 遍歷字符串(獲取字符串中的每一個字符)
*/
public class StringTest {
  public static void main(String[] args) {
      //創(chuàng)建一個字符串對象
      String s = "abcde";
      
      //原始做法
      System.out.println(s.charAt(0));
      System.out.println(s.charAt(1));
      System.out.println(s.charAt(2));
      System.out.println(s.charAt(3));
      System.out.println(s.charAt(4));
      System.out.println("---------");
      
      //用for循環(huán)改進(jìn)
      for(int x=0; x<5; x++) {
          System.out.println(s.charAt(x));
      }
      System.out.println("---------");
      
      //用length()方法獲取字符串的長度
      for(int x=0; x<s.length(); x++) {
          System.out.println(s.charAt(x));
      }
  }
}
  • 案例代碼七:
package com.neuedu.demo;
import java.util.Scanner;
/*
* 統(tǒng)計一個字符串中大寫字母字符,小寫字母字符,數(shù)字字符出現(xiàn)的次數(shù)。(不考慮其他字符)
* 
* 分析:
*         A:鍵盤錄入一個字符串?dāng)?shù)據(jù)
*         B:定義三個統(tǒng)計變量,初始化值都是0
*         C:遍歷字符串,得到每一個字符
*         D:拿字符進(jìn)行判斷
*             假如ch是一個字符。
*             大寫:ch>='A' && ch<='Z'
*             小寫:ch>='a' && ch<='z'
*             數(shù)字:ch>='0' && ch<='9'
*         E:輸出結(jié)果
*/
public class StringTest2 {
  public static void main(String[] args) {
      //鍵盤錄入一個字符串?dāng)?shù)據(jù)
      Scanner sc = new Scanner(System.in);
      System.out.println("請輸入一個字符串?dāng)?shù)據(jù):");
      String s = sc.nextLine();
      
      //定義三個統(tǒng)計變量,初始化值都是0
      int bigCount = 0;
      int smallCount = 0;
      int numberCount = 0;
      
      //遍歷字符串,得到每一個字符
      for(int x=0; x<s.length(); x++) {
          char ch = s.charAt(x);
          //拿字符進(jìn)行判斷
          if(ch>='A' && ch<='Z') {
              bigCount++;
          }else if(ch>='a' && ch<='z') {
              smallCount++;
          }else if(ch>='0' && ch<='9') {
              numberCount++;
          }else {
              System.out.println("該字符"+ch+"非法");
          }
      }
      
      //輸出結(jié)果
      System.out.println("大寫字符:"+bigCount+"個");
      System.out.println("小寫字符:"+smallCount+"個");
      System.out.println("數(shù)字字符:"+numberCount+"個");
  }
}
  • String類的轉(zhuǎn)換功能
  • 轉(zhuǎn)換方法演示
    char[] toCharArray():把字符串轉(zhuǎn)換為字符數(shù)組
    String toLowerCase():把字符串轉(zhuǎn)換為小寫字符串
    String toUpperCase():把字符串轉(zhuǎn)換為大寫字符串
  • 案例代碼八:
package com.neuedu.demo;
/*
* String類的轉(zhuǎn)換功能:
* char[] toCharArray():把字符串轉(zhuǎn)換為字符數(shù)組
* String toLowerCase():把字符串轉(zhuǎn)換為小寫字符串
* String toUpperCase():把字符串轉(zhuǎn)換為大寫字符串
* 
* 字符串的遍歷:
*         A:length()加上charAt()
*         B:把字符串轉(zhuǎn)換為字符數(shù)組,然后遍歷數(shù)組
*/
public class StringDemo {
  public static void main(String[] args) {
      //創(chuàng)建字符串對象
      String s = "abcde";
      
      //char[] toCharArray():把字符串轉(zhuǎn)換為字符數(shù)組
      char[] chs = s.toCharArray();
      for(int x=0; x<chs.length; x++) {
          System.out.println(chs[x]);
      }
      System.out.println("-----------");
      
      //String toLowerCase():把字符串轉(zhuǎn)換為小寫字符串
      System.out.println("HelloWorld".toLowerCase());
      //String toUpperCase():把字符串轉(zhuǎn)換為大寫字符串
      System.out.println("HelloWorld".toUpperCase());
  }
}
  • 轉(zhuǎn)換功能案例
  • 案例代碼九:
package com.neuedu.demo;
import java.util.Scanner;

/*
* 鍵盤錄入一個字符串,把該字符串的首字母轉(zhuǎn)成大寫,其余為小寫。(只考慮英文大小寫字母字符)
* 
* 分析:
*         A:鍵盤錄入一個字符串
*         B:截取首字母
*         C:截取除了首字母以外的字符串
*         D:B轉(zhuǎn)大寫+C轉(zhuǎn)小寫
*         E:輸出即可
*/
public class StringTest {
  public static void main(String[] args) {
      //鍵盤錄入一個字符串
      Scanner sc = new Scanner(System.in);
      System.out.println("請輸入一個字符串:");
      String s = sc.nextLine();
      
      //截取首字母
      String s1 = s.substring(0, 1);
      
      //截取除了首字母以外的字符串
      String s2 = s.substring(1);
      
      //B轉(zhuǎn)大寫+C轉(zhuǎn)小寫
      String s3 = s1.toUpperCase()+s2.toLowerCase();
      
      //輸出即可
      System.out.println("s3:"+s3);
  }
}
  • String類的其它功能
  • 其它方法演示
  • 案例代碼十:
package com.neuedu.demo;
/*
* 去除字符串兩端空格   
*     String trim()
* 按照指定符號分割字符串 
*     String[] split(String str)
*/
public class StringDemo {
  public static void main(String[] args) {
      //創(chuàng)建字符串對象
      String s1 = "helloworld";
      String s2 = "  helloworld  ";
      String s3 = "  hello  world  ";
      System.out.println("---"+s1+"---");
      System.out.println("---"+s1.trim()+"---");
      System.out.println("---"+s2+"---");
      System.out.println("---"+s2.trim()+"---");
      System.out.println("---"+s3+"---");
      System.out.println("---"+s3.trim()+"---");
      System.out.println("-------------------");
                                                              
      //String[] split(String str)
      //創(chuàng)建字符串對象
      String s4 = "aa,bb,cc";
      String[] strArray = s4.split(",");
      for(int x=0; x<strArray.length; x++) {
          System.out.println(strArray[x]);
      }
  }
}
  • String類的其它案例
  • 案例代碼十一:
package com.neuedu.demo;
/*
* 把數(shù)組中的數(shù)據(jù)按照指定個格式拼接成一個字符串
* 舉例:int[] arr = {1,2,3}; 
* 輸出結(jié)果:[1, 2, 3]
* 
* 分析:
*         A:定義一個int類型的數(shù)組
*         B:寫方法實(shí)現(xiàn)把數(shù)組中的元素按照指定的格式拼接成一個字符串
*         C:調(diào)用方法
*         D:輸出結(jié)果
*/
public class StringTest {
  public static void main(String[] args) {
      //定義一個int類型的數(shù)組
      int[] arr = {1,2,3};
      
      //寫方法實(shí)現(xiàn)把數(shù)組中的元素按照指定的格式拼接成一個字符串
      
      //調(diào)用方法
      String s = arrayToString(arr);
      
      //輸出結(jié)果
      System.out.println("s:"+s);
  }
  
  /*
   * 兩個明確:
   *      返回值類型:String
   *      參數(shù)列表:int[] arr
   */
  public static String arrayToString(int[] arr) {
      String s = "";
      
      //[1, 2, 3]
      s += "[";
      for(int x=0; x<arr.length; x++) {
          if(x==arr.length-1) {
              s += arr[x];
          }else {
              s += arr[x];
              s += ", ";
          }
      }
      s += "]";
      return s;
  }
}
  • 案例代碼十二:
package com.neuedu.demo;
import java.util.Scanner;
/*
* 字符串反轉(zhuǎn)
* 舉例:鍵盤錄入”abc”        
* 輸出結(jié)果:”cba”
* 
* 分析:
*         A:鍵盤錄入一個字符串
*         B:寫方法實(shí)現(xiàn)字符串的反轉(zhuǎn)
*             a:把字符串倒著遍歷,得到的每一個字符拼接成字符串。
*             b:把字符串轉(zhuǎn)換為字符數(shù)組,然后對字符數(shù)組進(jìn)行反轉(zhuǎn),最后在把字符數(shù)組轉(zhuǎn)換為字符串
*         C:調(diào)用方法
*         D:輸出結(jié)果
*/
public class StringTest2 {
  public static void main(String[] args) {
      //鍵盤錄入一個字符串
      Scanner sc = new Scanner(System.in);
      System.out.println("請輸入一個字符串:");
      String s = sc.nextLine();
      
      //寫方法實(shí)現(xiàn)字符串的反轉(zhuǎn)
      
      //調(diào)用方法
      String result = reverse(s);
      
      //輸出結(jié)果
      System.out.println("result:"+result);
  }
  
  
  
  /*
   * 把字符串倒著遍歷,得到的每一個字符拼接成字符串。
   * 
   * 兩個明確:
   *      返回值類型:String
   *      參數(shù)列表:String s
   */
  
  /*
  public static String reverse(String s) {
      String ss = "";
      
      for(int x=s.length()-1; x>=0; x--) {
          ss += s.charAt(x);
      }
      
      return ss;
  }
  */
  
  //把字符串轉(zhuǎn)換為字符數(shù)組,然后對字符數(shù)組進(jìn)行反轉(zhuǎn),最后在把字符數(shù)組轉(zhuǎn)換為字符串
  public static String reverse(String s) {
      //把字符串轉(zhuǎn)換為字符數(shù)組
      char[] chs = s.toCharArray();
      
      //對字符數(shù)組進(jìn)行反轉(zhuǎn)
      for(int start=0,end=chs.length-1; start<=end; start++,end--) {
          char temp = chs[start];
          chs[start] = chs[end];
          chs[end] = temp;
      }
      
      //最后在把字符數(shù)組轉(zhuǎn)換為字符串
      String ss = new String(chs);
      return ss;
  }
}
三、StringBuilder類
  • StringBuilder類概述

StringBuilder:是一個可變的字符串。字符串緩沖區(qū)類。

  • String和StringBuilder的區(qū)別:
    • String的內(nèi)容是固定的
    • StringBuilder的內(nèi)容是可變的
  • +=拼接字符串耗費(fèi)內(nèi)存原因:
    每次拼接都會產(chǎn)生新的字符串對象,而利用StringBuilder來拼接字符串自始至終用的都是同一個StringBuilder容器


    內(nèi)存.png
  • StringBuilder類的常用方法
  • A:構(gòu)造方法:
    StringBuilder()
  • B:成員方法:
    public int capacity():返回當(dāng)前容量 (理論值)
    public int length():返回長度(已經(jīng)存儲的字符個數(shù))
    public StringBuilder append(任意類型):添加數(shù)據(jù),并返回自身對象
    public StringBuilder reverse():反轉(zhuǎn)功能
  • 案例代碼十三:
package com.neuedu.demo;
/*
* StringBuilder:是一個可變的字符串。字符串緩沖區(qū)類。
* 
* String和StringBuilder的區(qū)別:
*         String的內(nèi)容是固定的。
*         StringBuilder的內(nèi)容是可變的。
* 
* 構(gòu)造方法:
*         StringBuilder()
* 
* 成員方法:
*         public int capacity():返回當(dāng)前容量
*         public int length():返回長度(字符數(shù))
* 
*         容量:理論值
*         長度:實(shí)際值
*/
public class StringBuilderDemo {
  public static void main(String[] args) {
      //創(chuàng)建對象
      StringBuilder sb = new StringBuilder();
      System.out.println("sb:"+sb);
      System.out.println("sb.capacity():"+sb.capacity());
      System.out.println("sb.length():"+sb.length());
  }
}  
  • 案例代碼十四:
package com.neuedu.demo;
/*
* 添加功能
*     public StringBuilder append(任意類型):添加數(shù)據(jù),并返回自身對象
* 反轉(zhuǎn)功能
*     public StringBuilder reverse()
*/
public class StringBuilderDemo {
  public static void main(String[] args) {
      //創(chuàng)建對象
      StringBuilder sb = new StringBuilder();
      
      //public StringBuilder append(任意類型)
      //StringBuilder sb2 = sb.append("hello");
      
      /*
      System.out.println("sb:"+sb);
      System.out.println("sb2:"+sb2);
      System.out.println(sb == sb2); //true
      */
      
      /*
      sb.append("hello");
      sb.append("world");
      sb.append(true);
      sb.append(100);
      */
      
      //鏈?zhǔn)骄幊?      sb.append("hello").append("world").append(true).append(100);
      
      System.out.println("sb:"+sb);
      
      //public StringBuilder reverse()
      sb.reverse();
      System.out.println("sb:"+sb);
      
  }
}
  • StringBuilder案例
  • 案例一需求:
    StringBuilder和String通過方法完成相互轉(zhuǎn)換
  • 案例代碼十五:
package com.neuedu.demo;
/*
* StringBuilder和String的相互轉(zhuǎn)換
* 
* StringBuilder -- String
*         public String toString():通過toString()就可以實(shí)現(xiàn)把StringBuilder轉(zhuǎn)成String
* 
* String -- StringBuilder
*         StringBuilder(String str):通過構(gòu)造方法就可以實(shí)現(xiàn)把String轉(zhuǎn)成StringBuilder
*/
public class StringBuilderTest {
  public static void main(String[] args) {
      //StringBuilder -- String
      /*
      StringBuilder sb = new StringBuilder();
      sb.append("hello").append("world");
      
      String s = sb.toString();
      System.out.println(s);
      */
      
      //String -- StringBuilder
      String s = "helloworld";
      StringBuilder sb = new StringBuilder(s);
      System.out.println(sb);
  }
}
  • 案例二需求:
    利用StringBuilder把數(shù)組拼接成一個字符串
    • 舉例:
      int[] arr = {1,2,3};
    • 結(jié)果:
      [1, 2, 3]
  • 案例代碼十六:
package com.neuedu.demo;
/*
* 把數(shù)組拼接成一個字符串
* 舉例:
*         int[] arr = {1,2,3};
* 結(jié)果:
*         [1, 2, 3]
*/
public class StringBuilderTest2 {
  public static void main(String[] args) {
      //定義一個數(shù)組
      int[] arr = {1,2,3};
      
      //寫方法實(shí)現(xiàn)拼接
      
      //調(diào)用方法
      String s = arrayToString(arr);
      
      //輸出結(jié)果
      System.out.println("s:"+s);
  }
  
  /*
   * 兩個明確:
   *      返回值類型:String
   *      參數(shù)列表:int[] arr
   */
  public static String arrayToString(int[] arr) {
      StringBuilder sb = new StringBuilder();
      //[1, 2, 3]
      sb.append("[");
      for(int x=0; x<arr.length; x++) {
          if(x==arr.length-1) {
              sb.append(arr[x]);
          }else {
              sb.append(arr[x]).append(", ");
          }
      }
      sb.append("]");
      
      String result = sb.toString();
      
      return result;
  }
}
  • 案例三需求:

利用StringBuilder完成字符串反轉(zhuǎn)

  • 案例代碼十七:
package com.neuedu.demo;
import java.util.Scanner;
/*
* 把字符串反轉(zhuǎn)
* 
* 分析:
*         A:鍵盤錄入一個字符串
*         B:寫方法實(shí)現(xiàn)反轉(zhuǎn)
*             String -- StringBuilder -- reverse() -- String
*         C:調(diào)用方法
*         D:輸出結(jié)果
*/
public class StringBuilderTest3 {
  public static void main(String[] args) {
      //鍵盤錄入一個字符串
      Scanner sc = new Scanner(System.in);
      System.out.println("請輸入一個字符串:");
      String s = sc.nextLine();
      
      //寫方法實(shí)現(xiàn)反轉(zhuǎn)
      
      //調(diào)用方法
      String result = myReverse(s);
      
      //輸出結(jié)果
      System.out.println("result:"+result);
  }
  
  /*
   * 兩個明確:
   *      返回值類型:String
   *      參數(shù)列表:String
   */
  public static String myReverse(String s) {
      //String -- StringBuilder -- reverse() -- String
      StringBuilder sb = new StringBuilder(s);
      sb.reverse();
      String result = sb.toString();
      return result;
  }
}
  • 案例四需求:
    判斷一個字符串是否是對稱字符串
    例如"abc"不是對稱字符串,"aba"、"abba"、"aaa"、"mnanm"是對稱字符串
  • 案例代碼十八:
package com.neuedu.demo;
import java.util.Scanner;
/*
* 判斷一個字符串是否是對稱字符串
* 例如"abc"不是對稱字符串,"aba"、"abba"、"aaa"、"mnanm"是對稱字符串
*
* 分析:
*  A:鍵盤錄入一個字符串
*  B:寫方法實(shí)現(xiàn)判斷一個字符串是否是對稱字符串
* 把字符串反轉(zhuǎn),和反轉(zhuǎn)前的字符串進(jìn)行比較,如果內(nèi)容相同,就說明是對稱字符串
*  C:調(diào)用方法
*  D:輸出結(jié)果
*/
public class StringBuilderTest4 {
public static void main(String[] args) {
//鍵盤錄入一個字符串
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個字符串:");
String s = sc.nextLine();
//寫方法實(shí)現(xiàn)判斷一個字符串是否是對稱字符串
//調(diào)用方法
boolean b = isSymmetry(s);
//輸出結(jié)果
System.out.println("b:"+b);
}
/*
* 兩個明確:
* 返回值類型:boolean
* 參數(shù)列表:String s
*/
public static boolean isSymmetry(String s) {
//把字符串反轉(zhuǎn),和反轉(zhuǎn)前的字符串進(jìn)行比較,如果內(nèi)容相同,就說明是對稱字符串
StringBuilder sb = new StringBuilder(s);
sb.reverse();
String result = sb.toString();
return result.equals(s);
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    開心的鑼鼓閱讀 3,393評論 0 9
  • 50道經(jīng)典Java編程練習(xí)題,將數(shù)學(xué)思維運(yùn)用到編程中來。抱歉哈找不到文章的原貼了,有冒犯的麻煩知會聲哈~ 1.指數(shù)...
    OSET我要編程閱讀 7,284評論 0 9
  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    葉總韓閱讀 5,225評論 0 41
  • 本文是我自己在秋招復(fù)習(xí)時的讀書筆記,整理的知識點(diǎn),也是為了防止忘記,尊重勞動成果,轉(zhuǎn)載注明出處哦!如果你也喜歡,那...
    波波波先森閱讀 958評論 1 6
  • 因?yàn)楹闷?,我們才會去探索;因?yàn)樘剿?,我們更加好奇。好奇心帶我們看更美的風(fēng)景。 一、一臺發(fā)動機(jī) 【認(rèn)知需求】 好奇的...
    靜心觀情閱讀 305評論 2 2

友情鏈接更多精彩內(nèi)容