文档版本 | 开发工具 | 测试平台 | 工程名字 | 日期 | 作者 | 备注 |
---|---|---|---|---|---|---|
V1.0 | 2016.03.03 | lutianfei | none |
StringBuffer
供我们使用。构造方法
StringBuffer的方法:
public class StringBufferDemo {
public static void main(String[] args) {
// public StringBuffer():无参构造方法
StringBuffer sb = new StringBuffer();
System.out.println("sb:" + sb);
System.out.println("sb.capacity():" + sb.capacity());
System.out.println("sb.length():" + sb.length());
System.out.println("--------------------------");
// public StringBuffer(int capacity):指定容量的字符串缓冲区对象
StringBuffer sb2 = new StringBuffer(50);
System.out.println("sb2:" + sb2);
System.out.println("sb2.capacity():" + sb2.capacity());
System.out.println("sb2.length():" + sb2.length());
System.out.println("--------------------------");
// public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("sb3:" + sb3);
System.out.println("sb3.capacity():" + sb3.capacity());
System.out.println("sb3.length():" + sb3.length());
}
}
/* 运行结果: sb: sb.capacity():16 sb.length():0 -------------------------- sb2: sb2.capacity():50 sb2.length():0 -------------------------- sb3:hello sb3.capacity():21 // 16+5 sb3.length():5 */
* 添加*功能
public StringBuffer** append
**(String str)
字符串缓冲区
本身 // 创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();
//public StringBuffer append(String str)
StringBuffer sb2 = sb.append("hello");//将hello添加到StringBuffer缓冲器
System.out.println("sb:" + sb);
System.out.println("sb2:" + sb2);
System.out.println(sb == sb2); // true
StringBuffer sb = new StringBuffer();
// 链式编程
sb.append("hello").append(true).append(12).append(34.56);
System.out.println("sb:" + sb);
sb.insert(5, "world");
System.out.println("sb:" + sb);
insert
(int offset,String str) 删除功能
deleteCharAt
(int index) public StringBuffer delete
(int start,int end)
// public StringBuffer delete(int start,int
// end):删除从指定位置开始指定位置结束的内容,并返回本身
// 需求:我要删除world这个字符串
// sb.delete(5, 10);
// 需求:我要删除所有的数据
sb.delete(0, sb.length());
System.out.println("sb:" + sb);
replace
(int start,int end,String str) java
// 需求:我要把world这个数据替换为"节日快乐"
sb.replace(5, 10, "节日快乐");
System.out.println("sb:" + sb);
反转功能
reverse
() // 添加数据
sb.append("霞青林爱我");
System.out.println("sb:" + sb);
// public StringBuffer reverse()
sb.reverse();
System.out.println("sb:" + sb);
/* result: sb:霞青林爱我 sb:我爱林青霞 */
截取功能 : 注意返回值类型不再是StringBuffer
本身,而是String类型
substring
(int start)substring
(int start,int end) String类型
,本身没有发生改变 public class StringBufferDemo {
public static void main(String[] args) {
// 创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();
// 添加元素
sb.append("hello").append("world").append("java");
System.out.println("sb:" + sb);
// 截取功能
// public String substring(int start)
String s = sb.substring(5);
System.out.println("s:" + s);
System.out.println("sb:" + sb);
// public String substring(int start,int end)
String ss = sb.substring(5, 10);
System.out.println("ss:" + ss);
System.out.println("sb:" + sb);
}
}
String
转StringBuffer
方法: 构造方法
append()
方法 StringBuffer
转String
方法: 构造方法
toString()
方法 public class StringBufferTest {
public static void main(String[] args) {
// String -- StringBuffer
String s = "hello";
StringBuffer sb2 = new StringBuffer();
sb2.append(s);
System.out.println("sb:" + sb);
System.out.println("sb2:" + sb2);
System.out.println("---------------");
// StringBuffer -- String
StringBuffer buffer = new StringBuffer("java");
// String(StringBuffer buffer)
// 方式1:通过构造方法
String str = new String(buffer);
// 方式2:通过toString()方法
String str2 = buffer.toString();
System.out.println("str:" + str);
System.out.println("str2:" + str2);
}
}
public class StringBufferTest2 {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 44, 33, 55, 11, 22 };
//用StringBuffer做拼接的方式
String s2 = arrayToString2(arr);
System.out.println("s2:" + s2);
}
// 用StringBuffer做拼接的方式
public static String arrayToString2(int[] arr) {
StringBuffer sb = new StringBuffer();
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("]");
return sb.toString();
}
}
public class StringBufferTest3 {
public static void main(String[] args) {
// 键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据:");
String s = sc.nextLine();
// 方式1:用String做拼接
String s1 = myReverse(s);
System.out.println("s1:" + s1);
// 方式2:用StringBuffer的reverse()功能
String s2 = myReverse2(s);
System.out.println("s2:" + s2);
}
// 用StringBuffer的reverse()功能
public static String myReverse2(String s) {
// StringBuffer sb = new StringBuffer();
// sb.append(s);
// StringBuffer sb = new StringBuffer(s);
// sb.reverse();
// return sb.toString();
// 简易版
return new StringBuffer(s).reverse().toString();
}
// 用String做拼接
public static String myReverse(String s) {
String result = "";
char[] chs = s.toCharArray();
for (int x = chs.length - 1; x >= 0; x--) {
// char ch = chs[x];
// result += ch;
result += chs[x];
}
return result;
}
}
public class StringBufferTest4 {
public static void main(String[] args) {
// 创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine();
// 一个一个的比较
boolean b = isSame(s);
System.out.println("b:" + b);
//用字符串缓冲区的反转功能
boolean b2 = isSame2(s);
System.out.println("b2:"+b2);
}
public static boolean isSame2(String s) {
return new StringBuffer(s).reverse().toString().equals(s);
}
public static boolean isSame(String s) {
boolean flag = true;
// 把字符串转成字符数组
char[] chs = s.toCharArray();
for (int start = 0, end = chs.length - 1; start <= end; start++, end--) {
if (chs[start] != chs[end]) {
flag = false;
break;
}
}
return flag;
}
}
通过查看API了解一下StringBuilder类
StringBuffer
兼容的 API,但不保证同步。该类被设计用作 StringBuffer
的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer
要快。String , StringBuffer , StringBuilder的区别
String
是内容不可变的,而StringBuffer,StringBuilder都是内容可变的。StringBuffer
是同步的,数据安全,效率低;StringBuilder
是不同步的,数据不安全,效率高StringBuffer
和数组
的区别?
看程序写结果:
String
作为参数传递,效果和基本类型
作为参数传递是一样的。StringBuffer
类型调用append
方法时内存值会改变public class StringBufferDemo {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
System.out.println(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;//StringBuffer类型用`=`赋值的特殊点。
sb2.append(sb1);
}
public static void change(String s1, String s2) {
s1 = s2;
s2 = s1 + s2;
}
}
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);
//由于我可能有多个数组要排序,所以我要写成方法
bubbleSort(arr);
System.out.println("排序后:");
printArray(arr);
}
//冒泡排序代码
public static void bubbleSort(int[] arr){
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
}
// 遍历功能
public static void printArray(int[] arr) {
System.out.print("[");
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
System.out.print(arr[x]);
} else {
System.out.print(arr[x] + ", ");
}
}
System.out.println("]");
}
}
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);
//用方法改进
selectSort(arr);
System.out.println("排序后:");
printArray(arr);
}
public static void selectSort(int[] arr){
for(int x=0; x<arr.length-1; x++){
for(int y=x+1; y<arr.length; y++){
if(arr[y] <arr[x]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
// 遍历功能
public static void printArray(int[] arr) {
System.out.print("[");
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
System.out.print(arr[x]);
} else {
System.out.print(arr[x] + ", ");
}
}
System.out.println("]");
}
}
public class ArrayTest {
public static void main(String[] args) {
// 定义一个字符串
String s = "dacgebf";
// 把字符串转换为字符数组
char[] chs = s.toCharArray();
// 把字符数组进行排序
bubbleSort(chs);
//把排序后的字符数组转成字符串
String result = String.valueOf(chs);
//输出最后的字符串
System.out.println("result:"+result);
}
// 冒泡排序
public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
}
public static int getIndex(int[] arr,int value) {
int index = -1;
for(int x=0; x<arr.length; x++) {
if(arr[x] == value) {
index = x;
break;
}
}
return index;
}
public class ArrayDemo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {11,22,33,44,55,66,77};
//写功能实现
int index = getIndex(arr, 33);
System.out.println("index:"+index);
//假如这个元素不存在后有什么现象呢?
index = getIndex(arr, 333);
System.out.println("index:"+index);
}
/* * 两个明确: * 返回值类型:int * 参数列表:int[] arr,int value */
public static int getIndex(int[] arr,int value){
//定义最大索引,最小索引
int max = arr.length -1;
int min = 0;
//计算出中间索引
int mid = (max +min)/2;
//拿中间索引的值和要查找的值进行比较
while(arr[mid] != value){
if(arr[mid]>value){
max = mid - 1;
}else if(arr[mid]<value){
min = mid + 1;
}
//加入判断
if(min > max){
return -1;
}
mid = (max +min)/2;
}
return mid;
}
}
排序
,查找
等功能。基本类型
和包装类
的对应 int
的值int
类型和 String
类型之间互相转换,还提供了处理 int
类型时非常有用的其他一些常量和方法。int类型和String类型的相互转换:String.valueOf
int –-> String
int number = 100;
// 方式1
String s1 = "" + number;
System.out.println("s1:" + s1);
// 方式2(最优)
String s2 = String.valueOf(number);
System.out.println("s2:" + s2);
// 方式3
// int -- Integer -- String
Integer i = new Integer(number);
String s3 = i.toString();
System.out.println("s3:" + s3);
// 方式4
// public static String toString(int i)
String s4 = Integer.toString(number);
System.out.println("s4:" + s4);
System.out.println("-----------------");
String –-> int: Integer.parseInt
// String -- int
String s = "100";
// 方式1
// String --> Integer --> int
Integer ii = new Integer(s);
// public int intValue()
int x = ii.intValue();
System.out.println("x:" + x);
//方式2(非常重要)
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println("y:"+y);
public int intValue()
自动拆箱:把包装类类型转换为基本类型
JDK1.5以后,简化了定义方式。
public class IntegerDemo {
public static void main(String[] args) {
// 定义了一个int类型的包装类类型变量i
// Integer i = new Integer(100);
Integer ii = 100;
ii += 200;
System.out.println("ii:" + ii);
// 通过反编译后的代码
// Integer ii = Integer.valueOf(100); //自动装箱
// ii = Integer.valueOf(ii.intValue() + 200); //自动拆箱,再自动装箱
// System.out.println((new StringBuilder("ii:")).append(ii).toString());
Integer iii = null;
// NullPointerException
if (iii != null) {
iii += 1000;
System.out.println(iii);
}
}
}
public class IntegerDemo {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
System.out.println("-----------");
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);
System.out.println(i3.equals(i4));
System.out.println("-----------");
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);
System.out.println(i5.equals(i6));
System.out.println("-----------");
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);
System.out.println(i7.equals(i8));
// 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
// Integer ii = Integer.valueOf(127);
}
}
public static void main(String[] args) {
// 定义三个统计变量。
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
// 键盘录入一个字符串。
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
// 把字符串转换为字符数组。
char[] chs = line.toCharArray();
// 历字符数组获取到每一个字符
for (int x = 0; x < chs.length; x++) {
char ch = chs[x];
// 判断该字符
if (Character.isUpperCase(ch)) {
bigCount++;
} else if (Character.isLowerCase(ch)) {
smallCount++;
} else if (Character.isDigit(ch)) {
numberCount++;
}
}
// 输出结果即可
System.out.println("大写字母:" + bigCount + "个");
System.out.println("小写字母:" + smallCount + "个");
System.out.println("数字字符:" + numberCount + "个");
}
}