十三、StringBuffer

StringBuffer的含义:

  • StringBuffer是线程安全的可变字符串

String和StringBuffer的区别:

  • A、String的长度和内容是不可改变的,StringBuffer是可变的
  • B、StringBuffer拼接字符串在常量池中不会浪费过多的空间

StringBuffer的构造方法:

    public StringBuffer():无参构造方法

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

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

 1 public class StringBufferDemo_my {
 2     public static void main(String[] args) {
 3         //创建StringBuffer对象
 4         StringBuffer sb = new StringBuffer();
 5 //        public StringBuffer():无参构造方法
 6         System.out.println("sb:"+sb);//打印发现不是地址值,说明重写toString方法。
 7         System.out.println("长度是:"+sb.length());//0
 8         System.out.println("容量是:"+sb.capacity());//16,因为容量默认是16
 9         
10 //        public StringBuffer(int capacity):指定容量的字符串缓冲区对象
11         StringBuffer sb2 = new StringBuffer(50);
12         System.out.println("sb2:"+sb2);//打印发现不是地址值,说明重写toString方法。
13         System.out.println("sb2长度是:"+sb2.length());//0
14         System.out.println("sb2容量是:"+sb2.capacity());//50,改了容量了
15         
16 //        public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
17         StringBuffer sb3 = new StringBuffer("hello");
18         System.out.println("sb3:"+sb3);//打印发现不是地址值,说明重写toString方法。
19         System.out.println("sb3长度是:"+sb3.length());//5
20         System.out.println("sb3容量是:"+sb3.capacity());//21,16+5=21;
21         
22     }
23 }

StringBuffer的方法

  • append添加功能
 1 public class StringBufferDemo_my {
 2     public static void main(String[] args) {
 3         //创建StringBuffer对象
 4         StringBuffer sb = new StringBuffer();
 5         StringBuffer sb2 = sb.append("hello");
 6         System.out.println(sb);//hello
 7         System.out.println(sb2);//hello
 8         System.out.println("--------------");
 9         //为什么两个都是hello,因为StringBuffer创建了一个缓冲区,这个添加就是针对这一个缓冲区的
10         //并不会额外创建对象。返回的也是他自己本身
11         
12         //append可以链式编程,只不过是添加在字符串的后面
13         sb.append("java").append(true).append(32.23);
14         System.out.println(sb);
15         //insert可以在制定的字符串索引位置添加字符串
16         sb.insert(5, "world");
17         System.out.println(sb);
18     }
19 }
  • delete删除功能
 1 public class StringBufferDemo_my {
 2     public static void main(String[] args) {
 3         //创建StringBuffer对象
 4         StringBuffer sb = new StringBuffer();
 5         sb.append("hello").append("world").append("java");
 6         //删除特定索引处的字符
 7         sb.deleteCharAt(0);//elloworldjava
 8         System.out.println(sb);
 9         //删除一段索引的字符串
10         sb.delete(0, 4);//worldjava---为什么是worldjava而不是oworldjava因为java中这样的方法大部分是包左不包右
11         System.out.println(sb);
12     }
13 }
  • 替换功能replace
 1 /*
 2  * StringBuffer的替换功能:
 3  * public StringBuffer replace(int start,int end,String str):从start开始到end用str替换
 4  * 
 5  */
 6 public class StringBufferDemo_my {
 7     public static void main(String[] args) {
 8         //创建StringBuffer对象
 9         StringBuffer sb = new StringBuffer();
10         sb.append("hello").append("world").append("java");
11         //需求是把world替换成“节日快乐”
12         sb.replace(5, 10, "节日快乐");//hello节日快乐java。为什么是5到10,因为包左不包右。
13         System.out.println(sb);
14     }
15 }
  • 反转功能
 1 /*
 2  * StringBuffer的反转功能:
 3  * public StringBuffer reverse()
 4  */
 5 public class StringBufferDemo {
 6     public static void main(String[] args) {
 7         // 创建字符串缓冲区对象
 8         StringBuffer sb = new StringBuffer();
 9 
10         // 添加数据
11         sb.append("霞青林爱我");
12         System.out.println("sb:" + sb);
13 
14         // public StringBuffer reverse()
15         sb.reverse();
16         System.out.println("sb:" + sb);
17     }
18 }
  • 截取功能。substring
 1 /*
 2  * StringBuffer的截取功能:注意返回值类型不再是StringBuffer本身了
 3  * public String substring(int start)
 4  * public String substring(int start,int end)
 5  * 
 6  */
 7 public class StringBufferDemo_my {
 8     public static void main(String[] args) {
 9         //创建StringBuffer对象
10         StringBuffer sb = new StringBuffer();
11         sb.append("helloworldjava");
12         System.out.println(sb);
13         sb.substring(0, 5);
14         System.out.println(sb);//打印的是helloworldjava,因为返回的是String类型字符串
15         String s = sb.substring(0, 5);//我们用String字符串接受
16         System.out.println(s);//hello,打印发现截截取了hello字符串
17     }
18 }
  • 把数组转换成字符串
 1 /*
 2  * 把数组拼接成一个字符串
 3  */
 4 
 5 public class StringBufferTest2_my {
 6     public static void main(String[] args) {
 7         // 定义一个数组
 8         int[] arr = { 44, 22, 44, 32, 66 };
 9         int[] arr2 = {33,44,55,66,77,88};
10         // 用方法做拼接
11         String s1 = arrayTostring(arr);
12         System.out.println(s1);
13         
14         String s2 = arrayTostring(arr2);
15         System.out.println(s2);
16     }
17 
18     public static String arrayToString2(int[] arr) {
19         // 用StringBuffer同样是调方法
20         StringBuffer sb = new StringBuffer();
21         sb.append("[");
22         for(int i = 0; i<arr.length ; i++) {
23             if( i == arr.length -1) {
24                 sb.append(arr[i]);
25             }else {
26                 sb.append(arr[i]+" ,");
27             }
28         }
29         sb.append("]");
30         String s = sb.toString();
31         return s;
32     }
33 
34     public static String arrayTostring(int[] arr) {
35         String s = "";
36         s += "[";
37         for (int i = 0; i < arr.length; i++) {
38             if (i == arr.length - 1) {
39                 s += arr[i];
40             } else {
41                 s += arr[i] + " ,";
42 
43             }
44         }
45         s += "]";
46         return s;
47     }
48 }
  • 把一个字符串反转的两种方法
 1 import java.util.Scanner;
 2 
 3 /*
 4  * 把字符串反转
 5  * 
 6  * 方式一:字符串拼接
 7  *         用字符串做拼接,思想是写一个方法,定义一个空的字符串,然后利用toCharArray方法把字符串字符数组
 8  *         写一个for循环,倒着遍历,把字符数组的值都赋值给字符串,返回这个字符串。
 9  * 方式二:用StringBuffer
10  * 
11  */
12 public class StringBufferDemo3_my {
13     public static void main(String[] args) {
14         //创建键盘录入对象
15         Scanner sc = new Scanner(System.in);
16         //录入字符串
17         System.out.println("请输入数据");
18         String str = sc.nextLine();
19         
20         //用String做拼接
21         String s = myReverse(str);
22         System.out.println(s);
23         
24         
25         //利用stringBuffer
26         String str2 = myReverse2(str);
27         System.out.println(str2);
28         
29     }
30     public static String myReverse2(String s) {
31         StringBuffer sb = new StringBuffer();
32         sb.append(s);
33         sb.reverse();
34         return sb.toString();
35     }
36     
37     public static String myReverse(String s) {
38         String result= "";
39         char[] cha = s.toCharArray();
40         for( int i = cha.length-1; i>=0 ; i--) {
41             result += cha[i];
42         }
43         return result;
44     }
45     
46     
47 }
  • 判断一个字符串是不是对成的字符串
 1 import java.util.Scanner;
 2 
 3 /*
 4  * 判断一个字符串是不是对成的字符串
 5  * 
 6  */
 7 
 8 public class StringBufreTest4_my {
 9     public static void main(String[] args) {
10         //创建字符录入对象
11         Scanner sc = new Scanner(System.in);
12         //录入数据
13         System.out.println("请录入数据");
14         String str = sc.nextLine();
15         System.out.println(isSame(str));
16         
17     }
18     public static boolean isSame(String s) {
19         return new StringBuffer(s).reverse().toString().equals(s);
20     }
21 }
  • 下面再说几个关于StringBuffer的面试题
 1 /*
 2  * 面试题:
 3  * 1:String,StringBuffer,StringBuilder的区别?
 4  * A:String是内容不可变的,而StringBuffer,StringBuilder都是内容可变的。
 5  * B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高
 6  * 
 7  * 2:StringBuffer和数组的区别?
 8  * 二者都可以看出是一个容器,装其他的数据。
 9  * 但是呢,StringBuffer的数据最终是一个字符串数据。
10  * 而数组可以放置多种数据,但必须是同一种数据类型的。
11  * 
12  * 3:形式参数问题
13  * String作为参数传递
14  * StringBuffer作为参数传递 
15  * 
16  * 形式参数:
17  *         基本类型:形式参数的改变不影响实际参数
18  *         引用类型:形式参数的改变直接影响实际参数
19  * 
20  * 注意:
21  *         String作为参数传递,效果和基本类型作为参数传递是一样的。
22  */
23 public class StringBufferDemo {
24     public static void main(String[] args) {
25         String s1 = "hello";
26         String s2 = "world";
27         System.out.println(s1 + "---" + s2);// hello---world
28         change(s1, s2);
29         System.out.println(s1 + "---" + s2);// hello---world
30 
31         StringBuffer sb1 = new StringBuffer("hello");
32         StringBuffer sb2 = new StringBuffer("world");
33         System.out.println(sb1 + "---" + sb2);// hello---world
34         change(sb1, sb2);
35         System.out.println(sb1 + "---" + sb2);// hello---worldworld
36 
37     }
38 
39     public static void change(StringBuffer sb1, StringBuffer sb2) {
40         sb1 = sb2;
41         sb2.append(sb1);
42     }
43 
44     public static void change(String s1, String s2) {
45         s1 = s2;
46         s2 = s1 + s2;
47     }
48 }

猜你喜欢

转载自www.cnblogs.com/ssshhh/p/11026398.html