zoukankan      html  css  js  c++  java
  • 2020-02-29(观看视频笔记)

    1、StringBuffer:线程安全的可变字符串

      (1)线程安全

          安全 --- 同步 --- 数据是安全的

          不安全 --- 不同步 --- 效率高一些

      (2)StringBuffer 和 String 和区别?

          A:前者长度和内容可变,后者不可变。

          B:如果使用前者做字符串拼接,不会浪费太多资源。 

      (3)StringBuffer的构造方法:

          public StringBuffer():无参构造,其初始容量为16个字符。

          public StringBuffer(int capacity):指定容量的字符串缓冲区对象

          public StringBuffer(String str):指定字符串内容的字符串缓冲区对象

      (4)StringBuffer的方法:

          public int capacity():返回当前容量。理论值

          public int length():返回长度(字符数)。实际值

          

      (5)StringBuffer的功能:

          添加功能:

            public StringBuffer append(String str):可以把任意类型添加到字符串缓冲区里面

            public StringBuffer insert(int offser, String str):在指定位置把任意类型的数据插入到字符串缓冲区,并返回字符串缓冲区本身

          删除功能:

            public StringBuffer deleteCharAt(int dext):删除指定位置的字符,并返回本身

            public StringBUffer delete(int start, int end):删除从指定位置开始到指定位置结束的内容,并返回本身

          替换功能:

            public StringBuffer replace(int start, int end, String str):从指定位置开始到指定位置结束的内容,用str字符串替换

          反转功能:

            public StringBuffer reverse()

          截取功能:

            public String substring(int start)

            public String substring(int start, int end)

            注意:返回值类型不再是StringBuffer本身了

      (6)String 和StringBuffer的互相转换?

          String转StringBuffer:

            A:通过构造方法

            B:通过append()

          StringBuffer转String:

            A:通过构造方法

            B:通过toString()方法

      (7)面试题:

          <1>String,StringBuffer,StringBuilder的区别?

              A:String是内容不可变的,而StringBuffer,StringBuilder都是内容可变的。

              B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高。

          <2>StringBuilder和数组的区别?

              A:二者都可以看成是一个容器,装其他的数据。但是呢,StringBuffer的数据最终是一个字符串数据。而数组可以放置多种数据,但必须是同一种数据类型的。

          <3>形式参数问题:

            String作为参数传递

            StringBuffer作为参数传递

              例:public class StringBufferDemo {

                  public static void main(String[] args) {

                    String s1 = 'hello";

                    String s2 = "world";

                    System.out.print;n(s1 + "---" + s2);    //hello---world

                    change(s1, s2);

                    System.out.println(s1 + "---" + s2);    //hello---world

                    StringBuffer sb1 = new StringBuffer("hello");

                    StringBuffer sb2 = new StringBuffer("world");

                    System.out.println(sb1 + "---" sb2);    //hello---world

                    change(sb1, sb2);

                    System.out.println(sb1 + "---" + sb2);   //hello-worldworld

                  }

                

                  public static void change(StringBuffer sb1, StringBuffer sb2) {  

                    sb1 = sb2;

                    sb2.append(sb1);  //当StringBuffer作为参数时,调用方法,会改变原有的数值

                  }  

      

                  

                  public static void change(String s1, String s2) {    //String作为参数传递,相等于基本类型作为参数传递

                    s1 = s2;

                    s2 = s1 + s2;

                  } 

              }

          注意:String作为参数传递,效果和基本类型作为参数传递是一样的。

  • 相关阅读:
    c++ 两个set合并
    L2-2 小字辈 (25 分)
    L1-1 天梯赛座位分配
    c++ 用 0x3f3f3f3f 设定最大int值的优点
    Treap(树堆)(转)
    new一个二维数组(转)
    Laplacian matrix(转)
    寒假计划制定
    寒假集训日志(八,九,十)——浪浪浪
    寒假集训日志(七)——数据结构
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/12382293.html
Copyright © 2011-2022 走看看