(JAVA)StringBuffer类

可变数组:StringBuffer类(静态缓冲区)
提高字符串的操作效率
StringBuffer 底层实现原理是字符数组,没有final
StringBuffer,含有数组char[] value 默认长度是16
在JAVA中,数组是固定长度,一旦创建不能改变
StringBuffer 通过数组的赋值,实现数组的可变性,但数组本身不能改变长度
1.将任意数据类型,添加到缓冲区中append()
2.返回一个StringBuffer,因此可以使用方法调用链
3.将任意数据,插入到缓冲区的某一个索引上index(索引,插入内容)
4.缓冲区的删除方法:delete(int start, int end);deleteCharAt( int index);
5.修改缓冲区的单个字符setCharAt( int index ,char ch);
6.缓冲区字符串翻转,reverse()
7,缓冲区字符串截取,substring()返回的是String结构
8.将缓冲区变成不可变对象toString()
9.字符串对象与缓冲区对象互转
字符串变成缓冲区
String s = new String("asdf");
StringBuffer b = new StringBuffer();
b.append(s);缓冲区变成字符串
StringBuffer buffer = new StringBuffer("vbnm");
String string = buffer.toString();
注意返回值是String 要接收变量
/*
public final class StringBufferextends AbstractStringBuilderimplements java.io.Serializable, CharSequenceA cache of the last value returned by toString. Clearedwhenever the StringBuffer is modified.private transient char[] toStringCache;use serialVersionUID from JDK 1.0.2 for interoperability
static final long serialVersionUID = 3388685877147921107L;/*** Constructs a string buffer with no characters in it and an* initial capacity of 16 characters.public StringBuffer() {super(16);}/*** Constructs a string buffer with no characters in it and* the specified initial capacity.** @param      capacity  the initial capacity.* @exception  NegativeArraySizeException  if the {@code capacity}*               argument is less than {@code 0}.public StringBuffer(int capacity) {super(capacity);}/*** Constructs a string buffer initialized to the contents of the* specified string. The initial capacity of the string buffer is* {@code 16} plus the length of the string argument.** @param   str   the initial contents of the buffer.public StringBuffer(String str) {super(str.length() + 16);append(str);}/*** Constructs a string buffer that contains the same characters* as the specified {@code CharSequence}. The initial capacity of* the string buffer is {@code 16} plus the length of the* {@code CharSequence} argument.* <p>* If the length of the specified {@code CharSequence} is* less than or equal to zero, then an empty buffer of capacity* {@code 16} is returned.** @param      seq   the sequence to copy.* @since 1.5public StringBuffer(CharSequence seq) {this(seq.length() + 16);append(seq);}*/import java.util.Scanner;public class test {public static void main(String[] args) {//System.out.println(getStringCont());//System.out.println(getString_1());//System.out.println(getString_2());//getString_3();//System.out.println(getString_4());System.out.println(getString_5());}//定义一个共能,从一个字符串中查找另一个字符串出现的次数public static int getStringCont() {Scanner sc1 = new Scanner(System.in);System.out.println("请输入一个字符串:");String s1 = sc1.nextLine();Scanner sc2 = new Scanner(System.in);System.out.println("请输入要查找的内容:");String s2 = sc2.nextLine();//定义计数器int count = 0;//定义下角标int index = 0;if (s1.length() == 0 || s2.length() == 0)return count;else {//判断s1第一次出现的位置,只要第一次出现的位置大于零,就进入循环while((index = s1.indexOf(s2))>0){count++;//新的字符串长度为查找内容后的下角标到结尾s1 = s1.substring(index+s2.length());}}return count;}public static StringBuffer getString_1(){StringBuffer buffer = new StringBuffer("abc");buffer.append("sdf");buffer.insert(2,"bnm");return buffer;}public static StringBuffer getString_2(){StringBuffer buffer_1 = new StringBuffer();buffer_1.append("poiu");System.out.println(buffer_1+"1");//删除指定下角标buffer_1.deleteCharAt(1);System.out.println(buffer_1+"2");//删除一堆buffer_1.delete(0,2);return buffer_1;}public static void getString_3(){StringBuffer buffer_2 = new StringBuffer();buffer_2.append("abcdefg");buffer_2.setCharAt(3,'p');System.out.println(buffer_2);}public static StringBuffer getString_4(){String s = "asdf";StringBuffer b = new StringBuffer();b.append(s);return b;}public static String getString_5(){StringBuffer buffer = new StringBuffer("vbnm");String string = buffer.toString();return string;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421831.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

40个超酷的jQuery动画教程

原文地址&#xff1a;http://www.goodfav.com/40-cool-jquery-animation-tutorials-1189.html 由于它的到来&#xff0c;已经引起了jQuery的网络风暴&#xff0c;现在是创建漂亮的网页上的动画的首选解决方案之一。 jQuery提供了很大的跨浏览器支持&#xff0c;它是轻量级且易于…

好东西,资料!!

架构&#xff1a; http://www.cnblogs.com/lovecindywang/archive/2012/12/23/2829828.html http://www.cnblogs.com/angben/archive/2012/12/23/2829795.html http://blog.csdn.net/longxibendi/article/details/6628480 企业应用集成可以在不同层面上进行&#xff1a;例如在数…

(JAVA)StringBuffer类(练习)

package com.book.lite;import java.util.Scanner;/* 1.将数组拼接成字符串 2.自定义方法&#xff0c;将缓冲区翻转*/ public class StringBufferDemo2 {public static void main(String[] args){System.out.println(getString_1());System.out.println(getString_2());}public…

hdu2955 Robberies (动态规划之背包)

http://acm.hdu.edu.cn/showproblem.php?pid2955 题意&#xff1a;Roy想要抢劫银行&#xff0c;每家银行多有一定的金额和被抓到的概率&#xff0c;知道Roy被抓的最大概率P&#xff0c;求Roy在被抓的情况下&#xff0c;抢劫最多。 分析&#xff1a;被抓概率可以转换成安全概率…

数组排序之冒泡排序

package com.book.lite;/*** author zhangyu* date 2021年08月11日 10:51 下午* 实现整数数组的排序&#xff0c;顺序排序&#xff0c;冒泡排序*/ public class arrDome {public static void main(String[] a){int[] arr {2,0,9,8,67,-45,-199}; // smallNumber(arr); …

黑马Java学习笔记之-----集合框架

---------------------- android培训、java培训、期待与您交流&#xff01; ---------------------- 一&#xff0e;概述&#xff1a; Java的集合类是一种特别有用的工具类&#xff0c;它可以用于存储数量不等的多个对象&#xff08;实际上是对象的引用&#xff09;&#xff0c…

(JAVA)二分法

package com.book.lite;/*** author zhangyu* date 2021年08月12日 11:15 下午* 使用二分法&#xff0c;查找有序数组的某一个值*/ public class binarySeach {public static void main(String[] args){int[] arr {2,4,5,9,45,65,74,83,100};int index arrNumber(arr,0);Syst…

35岁前成功原则

第一章:一个目标 一艘没有航行目标的船&#xff0c;任何方向的风都是逆风 1、你为什么是穷人&#xff0c;第一点就是你没有立下成为富人的目标 2、你的人生核心目标是什么&#xff1f; 杰出人士与平庸之辈的根本差别并不是天赋、机遇&#xff0c;而在于有无目标。 3、起跑领先一…

(JAVA)Arrays数组工具类

package com.book.lite; /*** author zhangyu* date 2021年08月14日 10:52 下午* 数组的工具类&#xff0c;方法是静态的* sort() 排序* binarySearch() 二分查找* toString() 将数组变成字符串&#xff0c;是数组工具类独有的方法*/ import java.util.Arrays; public class Ar…

2013-05

Tom Cubie &#xff0c;CubieBoard&#xff08;类树莓派&#xff09;的设计者 http://www.fendou.info/tag/cubieboard/ LevelDB关键实现图解 http://www.wzxue.com/leveldb%E5%9B%BE%E8%A7%A3/ 电子商务系统的设计、开发、部署、运维及其解决方案 http://www.entlib.com/ 迷你…

(JAVA)基本数据类型 对象包装类

package com.book.lite;/*** author zhangyu* date 2021年08月15日 4:51 下午* 基本数据类型 对象包装类* 对八个基本数据类型&#xff0c;提供8个类&#xff0c;&#xff0c;将基本数据类型&#xff0c;封装成8个对象* byte Byte* short Short* int I…

作业自动提示功能设计思路

1、利用现在FLEX项目中的心跳包机制&#xff0c;使用SOCKET心跳包技术获取最新的作业情况。 2、在现在FLEX项目中有一个&#xff1a; 核心代码&#xff1a; 这样我们可以利用这个通道&#xff0c;获取相应的信息。 具体修改步骤如下&#xff1a; 1、准备工作 创建一张表&#x…

(JAVA)Integer类之基本数据类型之间的转换

package com.book.lite;/*** author zhangyu* date 2021年08月15日 8:01 下午* Integer类&#xff0c;其他常用方法&#xff1a;* 1.parseInt(String i) 将数字格式字符串&#xff0c;转换成基本数据类型* 2.parseInt(String i, int radix) 将数字类型字符串转换成进制数* 3.t…

java文件读写操作大全

转自http://blog.sina.com.cn/s/blog_4a9f789a0100ik3p.html 一.获得控制台用户输入的信息 1 public String getInputMessage() throws IOException...{2 System.out.println("请输入您的命令∶");3 byte buffer[]new byte[1024];4 …

(Java)Integer类的其他常用方法

package com.book.lite;/*** author zhangyu* date 2021年08月15日 8:01 下午* Integer类&#xff0c;其他常用方法&#xff1a;* 1.parseInt(String i) 将数字格式字符串&#xff0c;转换成基本数据类型* 2.parseInt(String i, int radix) 将数字类型字符串转换成进制数* 3.t…

libev源码分析--常用的watcher

在上一篇文章里&#xff0c;我们分析了libev整体设计思想和主循环的工作原理&#xff0c;也提到了watcher是衔接开发者代码的主要入口。watcher与开发者最接近&#xff0c;也与具体事件处理逻辑最接近。所以&#xff0c;watcher的具体实现&#xff0c;与性能的关系也相当密切。…

(Java)Character类

package com.book.lite;import sun.lwawt.macosx.CSystemTray;import java.util.Scanner;/*** author zhangyu* date 2021年08月16日 10:50 下午* Character类的方法* 1.判断是否小写&#xff1a;isLowerCase()* 2.判断是否大写&#xff1a;isUpperCase()* 3.判断是不是数字&am…

棋盘切割 DP POJ 1191

把方差公式先变形为 σ2 (1/n)∑xi2-xa2 xa为平均值。 由于要求标准差最小&#xff0c;只需方差最小&#xff0c;平均值都是一样的&#xff0c;n也是一样的&#xff0c;这样原问题就变为求这n快小棋盘总分的平方和最小 考虑左上角为&#xff08;x1,y1&#xff09;,右上角为&am…