Java入门-StringBuilder

一、创建StringBuilder对象

创建StringBuilder的对象,分别使用无参构造和有参构造
StringBuilder的四种构造方法:
    1.StringBuilder():
        构造一个不带任何字符的字符串生成器,其初始容量为 16 个字符

    2.StringBuilder(CharSequence seq):
        构造一个字符串生成器,它包含与指定的 CharSequence 相同的字符


    3.StringBuilder(int capacity):
        构造一个不带任何字符的字符串生成器,其初始容量由 capacity 参数指定


    4.StringBuilder(String str):
        构造一个字符串生成器,并初始化为指定的字符串内容

*要注意,一般而言,打印对象,一般输出的是地址值,但是这里我们打印stringBuilder输出的是字符串缓冲区中的内容
package com.tan.stringbuilder;

public class StringBuilderDemo {
    public static void main(String[] args) {
        /*
            使用无参构造创建StringBuilder的对象的时候,建的是一个空的、容量为16个字符的字符生成器
         */
        StringBuilder stringBuilder = new StringBuilder();
        System.out.println("stringBuilder.capacity() = " + stringBuilder.capacity());//stringBuilder.capacity() = 16
        System.out.println("stringBuilder = " + stringBuilder);//stringBuilder =


        /*
            第二种构造方法的源码:
            public StringBuilder(CharSequence seq) {
                this(seq.length() + 16);
                append(seq);
            }

            第四种构造方法的源码:
            public StringBuilder(String str) {
                super(str.length() + 16);
                append(str);
            }

            可以看到其实两者方法体里的代码基本一致
            接口CharSequence是String类的父接口,这两种构造方法最大的区别可能也就是第二种形参中
            CharSequence能接受更多类型的数据(只要是CharSequence的实现类都可以放)

         */
        System.out.println("====================");
        StringBuilder stringBuilder1 = new StringBuilder();
        System.out.println("stringBuilder1.capacity() = " + stringBuilder1.capacity());//stringBuilder1.capacity() =
        System.out.println("stringBuilder1 = " + stringBuilder1);//stringBuilder1 =


        /*
            向其传入一个int类型的值m,它的容量不再是初始容量16,而是咱自己传入的m大的容量的字符生成器
         */
        System.out.println("====================");
        StringBuilder stringBuilder2 = new StringBuilder(10);
        System.out.println("stringBuilder2.capacity() = " + stringBuilder2.capacity());//stringBuilder2.capacity() = 10
        System.out.println("stringBuilder2 = " + stringBuilder2);//stringBuilder2 =



        /*
            向其传入一个String类型的初始化值,它的容量不再是初始容量16,而是添加的初始化值所占的字符+16个字符
         */
        System.out.println("====================");
        StringBuilder stringBuilder3 = new StringBuilder("10");
        System.out.println("stringBuilder3.capacity() = " + stringBuilder3.capacity());//stringBuilder3.capacity() = 18
        System.out.println("stringBuilder3 = " + stringBuilder3);//stringBuilder3 = 10


    }
}

二、StringBuilder常用的方法

StringBuilder append():
1.形参可以传任何类型,因为在API中明确写了有StringBuilder append(Object obj)这一个方法,
而Object是超类,所以这里的形参是可以传任何类型的

2.StringBuilder append(A)实现的是拼接,将A接到原来StringBuilder对象的内容的后面

3.StringBuilder这里和String不一样,String只要有了变动,都是新建了个String对象,而StringBuilder
的append方法使用是直接在原StringBuilder对象上操作的

4.StringBuilder的append方法比String使用加法进行拼接节省很多空间
char charAt(int index) : 获取指定索引位置的字符
int length() VS int capacity()
1.length()返回的是当前已经装了几个字符
2.capacity()返回的是本StringBuilder能装几个字符
int indexOf(String str)  VS int lastIndexOf(String str)
1.indexOf(String str):返回传入的字符串在StringBuilder对象中第一次出现的索引位置
2.lastIndexOf(String str):返回传入的字符串在StringBuilder对象中最后一次出现的索引位置
StringBuilder reverse()
1.StringBuilder对象存储的内容进行反转
3.StringBuilder这里和String不一样,String只要有了变动,都是新建了个String对象,而StringBuilder
的reverse方法使用是直接在原StringBuilder对象上操作的
String substring(int start)  VS  String substring(int start, int end)
1.String substring(int start)从start开始截取,直到最后一个元素--->[start,last element]
2.String substring(int start, int end)从start开始截取,直到end--->[start,end)
String toString():StringBuilder对象转换成字符串对象

代码实现:

package com.tan.stringbuilder;

public class StringBuilderDemo {
    public static void main(String[] args) {
        /*
            StringBuilder append():
            1.形参可以传任何类型,因为在API中明确写了有StringBuilder append(Object obj)这一个方法,
            而Object是超类,所以这里的形参是可以传任何类型的

            2.StringBuilder append(A)实现的是拼接,将A接到原来StringBuilder对象的内容的后面

            3.StringBuilder这里和String不一样,String只要有了变动,都是新建了个String对象,而StringBuilder
            的append方法使用是直接在原StringBuilder对象上操作的

            4.StringBuilder的append方法比String使用加法进行拼接节省很多空间
         */
        StringBuilder stringBuilder = new StringBuilder("Hello");
        System.out.println("stringBuilder = " + stringBuilder);//stringBuilder = Hello
        stringBuilder.append("World");
        System.out.println("stringBuilder = " + stringBuilder);//stringBuilder = HelloWorld
        System.out.println("=================");

        /*
            char charAt(int index) : 获取指定索引位置的字符
         */
        System.out.println("stringBuilder.charAt(2) = " + stringBuilder.charAt(2));//stringBuilder.charAt(2) = l
        System.out.println("=================");

        /*
            int length() VS int capacity()
            1.length()返回的是当前已经装了几个字符
            2.capacity()返回的是本StringBuilder能装几个字符
         */
        System.out.println("stringBuilder.length() = " + stringBuilder.length());//stringBuilder.length() = 10
        System.out.println("stringBuilder.capacity() = " + stringBuilder.capacity());//stringBuilder.capacity() = 21
        System.out.println("=================");

        /*
            int indexOf(String str)  VS int lastIndexOf(String str)
            1.indexOf(String str):返回传入的字符串在StringBuilder对象中第一次出现的索引位置
            2.lastIndexOf(String str):返回传入的字符串在StringBuilder对象中最后一次出现的索引位置
         */
        System.out.println("stringBuilder.indexOf(\"l\") = " + stringBuilder.indexOf("l"));//stringBuilder.indexOf("l") = 2
        System.out.println("stringBuilder.lastIndexOf(\"l\") = " + stringBuilder.lastIndexOf("l"));//stringBuilder.lastIndexOf("l") = 8
        System.out.println("=================");

        /*
            StringBuilder reverse()
            1.将StringBuilder对象存储的内容进行反转
            3.StringBuilder这里和String不一样,String只要有了变动,都是新建了个String对象,而StringBuilder
            的reverse方法使用是直接在原StringBuilder对象上操作的
         */
        System.out.println("stringBuilder = " + stringBuilder);//stringBuilder = HelloWorld
        System.out.println("stringBuilder.reverse() = " + stringBuilder.reverse());//stringBuilder.reverse() = dlroWolleH
        System.out.println("=================");


        /*
            String substring(int start)  VS  String substring(int start, int end)
            1.String substring(int start)从start开始截取,直到最后一个元素--->[start,last element]
            2.String substring(int start, int end)从start开始截取,直到end--->[start,end)
         */
        System.out.println("stringBuilder.reverse() = " + stringBuilder.reverse());//stringBuilder = HelloWorld
        System.out.println("stringBuilder.substring(5) = " + stringBuilder.substring(5));//stringBuilder.substring(5) = World
        System.out.println("stringBuilder.substring(2,5) = " + stringBuilder.substring(2, 5));//stringBuilder.substring(2,5) = llo
        System.out.println("=================");

        /*
            String toString():将StringBuilder对象转换成字符串对象
         */
        System.out.println("stringBuilder.toString() = " + stringBuilder.toString());//stringBuilder.toString() = HelloWorld
        System.out.println("=================");

    }
}

三、StringBuilder的练习

问题描述:
按照指定格式打印字符串,格式样例:[a1,a2,a3]

代码实现:

package com.tan.stringbuilder;
import java.util.Arrays;
/*
Tips:
StringBuilder append()和reverse()都返回的自己本身
 */

public class StringBuilderDemo {
    public static void main(String[] args) {
        int[] arr = {1,3,5,7,9};
//        基于面向对象思想的一种方法:Arrays.toString(arr)
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));

//        基于面向过程思想的三种方法:1.手动实现 2.String的加法拼接 3.StringBuilder的append()
        
//        1.手动实现
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1){
                System.out.println(arr[i] + "]");
            }else{
                System.out.print(arr[i] + ", ");
            }
        }
        
//        2.String的加法拼接
        String strPrint = "[";
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1){
                strPrint += (arr[i] + "]");
            }else {
                strPrint += (arr[i] + ", ");
            }
        }
        System.out.println("strPrint = " + strPrint);

//        3.StringBuilder的append()
        StringBuilder stringBuilder = new StringBuilder("[");
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1){
                stringBuilder.append(arr[i] + "]");
            }else {
                stringBuilder.append(arr[i] + ", ");
            }
        }
        System.out.println("stringBuilder = " + stringBuilder);
    }
}

你可能感兴趣的:(Java,入门,java,开发语言,后端)