叨逼叨两句
加油!
13-(1-12):StringBuffer类
概述
- StringBuffer不能像String一样用“+”与任意类型进行连接来修改,它只能通过某些方法来进行修改。
- StringBuffer是一个可变的字符序列,而String是一个不可变的字符序列。
构造方法
- StringBuffer():构造一个不带字符的字符串缓冲器,其初始容量为16个字符。【这个16相当于容器容量】
- StringBuffer(int capacity):构造一个不带字符,但具有指定初始容量的字符串缓冲区。
- StringBuffer(String str):构造一个字符串缓冲区, 并将其内容初始化为指定字符串内容。
- StringBuffer(CharSequence seq):构造一个字符串缓冲区,它包含与指定的CharSequence相同的字符。
区分length()与capacity()
- 前者是容器中的字符个数。
- 后者是容器的容量
package test_heima;
public class Demo03 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.length());
System.out.println(sb.capacity());
/*StringBuffer sb = new StringBuffer("heima");
System.out.println(sb.length());// 5
System.out.println(sb.capacity());// 字符串的length+初始容量=21
*/
}
}
添加功能
-
public StringBuffer append(String str)
package test_heima; public class Demo04 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); StringBuffer sb2 = sb.append(true); StringBuffer sb3 = sb.append("hehehehe"); StringBuffer sb4 = sb.append(10000000); System.out.println(sb.toString()); System.out.println(sb2.toString()); System.out.println(sb3.toString()); System.out.println(sb4.toString()); //为什么结果一样?因为append方法返回的StringBuffer本身,即修改的是本身。 } }
-
public StringBuffer insert(int offset,String str)
package test_heima; public class Demo05 { public static void main(String[] args) { StringBuffer sb = new StringBuffer("0123456"); sb.insert(3, true); //sb.insert(1113, true); //在指定位置若无字符便会报错StringIndexOutOfBoundsException System.out.println(sb); } }
删除功能
-
public StringBuffer deleteCharAt(int index)
package test_heima; public class Demo06 { public static void main(String[] args) { StringBuffer sb = new StringBuffer("0123456"); //sb.deleteCharAt(3333); //报索引越界异常 sb.deleteCharAt(3); System.out.println(sb); } }
-
public StringBuffer delete(int start,int end)【包头不包尾】
package test_heima; public class Demo06 { public static void main(String[] args) { StringBuffer sb = new StringBuffer("0123456"); sb.delete(0, 2); //sb.delete(0, 222); //这个不会报错 System.out.println(sb); } }
替换和反转功能
-
public StringBuffer replace(int start,int end,String str)
package test_heima; public class Demo { public static void main(String[] args) { StringBuffer sb = new StringBuffer("0123456789"); sb.replace(0, 3, "apple"); System.out.println(sb); } }
-
public StringBuffer reverse()
package test_heima; public class Demo { public static void main(String[] args) { StringBuffer sb = new StringBuffer("0123456789"); sb.replace(0, 3, "apple"); System.out.println(sb); sb.reverse(); System.out.println(sb); } }
截取功能
- public String subString(int start)
- public String subString(int start,int end)
注意:返回类型不再是StringBuffer本身,而是String
package test_heima;
public class Demo07 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("012345678");
String cc = sb.substring(2);
System.out.println(cc);
}
}
package test_heima;
public class Demo07 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("012345678");
String cc = sb.substring(2,3);
System.out.println(cc);
}
}
StringBuffer与String的互相转换
-
String——>StringBuffer
- 通过构造方法
- 通过append()方法
-
StringBuffer——>String
- 通过构造方法
- 通过toString()方法
- 通过subString(0,length)
package test_heima;
public class Demo08 {
public static void main(String[] args) {
//String----->StringBuffer
//法1:构造方法
StringBuffer sb = new StringBuffer("abc");
System.out.println(sb);
//法2:apend
StringBuffer sb2 = new StringBuffer();
sb2.append("abc");
System.out.println(sb2);
//StringBuffer---->String
//法1:构造方法
StringBuffer sb3 = new StringBuffer("abc");
String cc = new String(sb3);
System.out.println(cc);
//法2:通过toString方法
StringBuffer sb4 = new StringBuffer("abc");
String kk = sb4.toString();
System.out.println(kk);
//法3:通过subString(0,length)
StringBuffer sb5 = new StringBuffer("abc");
String zz = sb5.substring(0,sb5.length());
System.out.println(zz);
}
}
把数组转成字符串
用String功能实现
- 这种方法不推荐,会不断在堆上产生垃圾。
package test_heima;
public class Demo09 {
public static void main(String[] args) {
int[] arr = {1,2,3};
String s = "[";
for(int i = 0;i<arr.length;i++){
if(i!=(arr.length-1)){
s += arr[i]+",";
} else {
s += arr[i]+"]";
}
}
System.out.println(s);
}
}
用StringBuffer功能实现
- 这种方法不推荐,变量与常量相互连接会新建StringBuffer对象,还得用其toString方法进行转换生成String对象,这样消耗内存
StringBuffer s = new StringBuffer();
s.append("[");
for(int i = 0;i<arr.length;i++){
if(i!=arr.length-1){
s.append(arr[i]+",");
} else {
s.append(arr[i]+"]");
}
}
System.out.println(s);
- 推荐这种方法,从头到尾就新建了一个StringBuffer对象
StringBuffer s = new StringBuffer();
s.append("[");
for(int i = 0;i<arr.length;i++){
if(i!=arr.length-1){
s.append(arr[i]).append(",");
} else {
s.append(arr[i]).append("]");
}
}
System.out.println(s);
StringBuilder与StringBuffer的区别
- StringBuffer是JDK1.0版本的,是线程安全的,同步的,效率低
- StringBuilder是JDK1.5版本的,是线程不安全的,不同步的,效率高
String与StringBuilder、StringBuffer的区别
- String是一个不可变的字符序列。
- StringBuilder、StringBuffer是可变的字符序列
String以StringBuffer作为参数传递
- 基本数据类型的值传递,不改变其值
- 引用数据类型的值传递,改变其值
- String类虽然是引用数据类型,但是它当做参数传递时和基本数据类型是一样的。
- 看看java 函数形参传值和传引用的区别
- java方法基本数据类型是传值,对象类型传引用,这是千真万确的。
- 当参数是对象时,无论方法体内进行了何种操作,都不会改变实参对象的引用。
- 当参数是对象时,只有在方法内部改变了对象的内容时,才会改变实参对象内容。【其实形参拿到的只是引用地址值的拷贝,这个拷贝与实参无关了,但是这个拷贝可以操作对象的方法,进而改变它的属性】
package test_heima;
public class Demo10 {
public static void main(String[] args) {
String s = "aaa";
System.out.println(s);
chang(s);
System.out.println(s);
StringBuffer cc = new StringBuffer();
cc.append("kkk");
System.out.println(cc);
chang(cc);
System.out.println(cc);
}
public static void chang(StringBuffer cc) {
cc.append("llll");
}
public static void chang(String s) {
s += "bbb";
}
}
13-(12-17):冒泡排序、选择排序、数组二分法查找
package test_heima;
public class Demo11 {
public static void main(String[] args) {
int[] arr = {23,765,12,3,24,55,67};
//bubbleSort(arr); //冒泡排序
//selectSort(arr); //选择排序
//print(arr);
int[] arr2 = {11,22,33,44,55,66};
int i = binarySearch(arr2,66);//二分法查找的前提是数组有序
System.out.println(i);
}
public static int binarySearch(int[] arr,int value){
int min = 0;
int max = arr.length - 1;
int mid = (min + max)/2;
while(arr[mid] != value){
if(arr[mid] < value){
min = mid + 1;
} else if (arr[mid] > value){
max = mid - 1;
} else {
return mid;
}
mid = (min + max)/2;
if(min > max){
return -1;
}
}
return mid;
}
public static void selectSort(int[] arr) {
for(int i = 0;i < arr.length - 1;i++){
for(int j = i +1;j < arr.length ;j++){
if(arr[i] < arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
public static void bubbleSort(int[] arr) {
//冒泡排序
boolean sorted = true;
for(int i = 0;i < arr.length-1;i++){
for(int j = 0;j < arr.length-1-i;j++){
if(arr[j]<arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
sorted = false;
}
}
if(sorted){
break;
}
}
}
public static void print(int[] arr){
for(int i = 0;i < arr.length; i++){
System.out.println(arr[i]+" ");
}
}
}
13.18:Array类的常用方法
- public static String toString(int[] a)
- public static void sort(int[] a);
- public static int binarySearch(int[] a,int key)
13.19:基本类型包装类概述
除了int和char的包装类分别对应于Integer和Character,其余的都是将类型名称改成首字母大写即可。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。