JAVA自学笔记13

JAVA自学笔记13

1、StringBuffer类
1)线程安全的可变字符序列
线程安全(即同步)
2)StringBuffer与String的区别:一个可变一个不可变
3)构造方法:
①public StringBuffer()
无参构造。
StringBuffer ssd=new StringBuffer();
Systrm.out.println(ssd);//已被重写,由于没有内容而为空
Systrm.out.println(ssd.capacity());//默认为16
Systrm.out.println(ssd.length);//0
②public StringBuffer(Int capacity)
指定容量的字符串缓冲区对象,容量即最大长度
③public StringBuffer(String str)
指定内容的字符串缓冲区对象
StringBuffer ssb=new StringBuffer(“abcd”);
Systrm.out.println(ssb.lcapacity);//输出为20,即=字符串长+16
4)添加功能
①pubilc StringBuffer append(String str);
可以把任意类型添加至字符串缓冲区中

StringBuffer sb=new StringBuffer();
StringBuffer sb2=sb.append("hello");

System.out.println(sb);//hello
System.out.println(sb2);//hello,这里sb=sb2.并没有开辟新空间
sb.appernd(1);
sb.appernd(two);
System.out.println(sb);//hello1two
//或链式编程:sb.append(1).append("two")

②pubilc StringBuffer insert(int offest,String str);
在指定位置把任意类型的数据插入到字符缓冲区中

5)删除功能:
①public StringBuffer deleteCharAt(int index)
删除指定索引处的字符,并返回删除后的字符串给自身
②public StringBuffer delete(int start,int end)
删除从指定位置开始到指定位置结束的字符串,并返回删除后的字符串给自身。注意,不删除索引为end的字符串,即包左不包右

6)替换功能:
public StringBuffer replace(int start,int end,String str)
将从指定位置开始到指定位置结束的字符串替换为指定字符串

7)反转功能
pubilc StringBuffer reverse()
将字符串顺序反转后返回给原字符串

8)截取功能:
public String substring(int start);
public String substring(int start,int end);
注意:将返回一个String类型,原字符串对象并未发生改变

9)String与StringBuffer的相互转换
String s=”abc”;
StringBuffer sb=”abc”//报错,不能把字符串的值直接赋给StringBuffer
StringBuffer sb=s;//同上

//由String向StringBuffer的转换
StringBuffer sb=new StringBuffer(s);//使用构造方法

StringBuffer sb2=new StringBuffer();
sb2.append(s);//也正确实现了上述转换

//由StringBuffer向String的转换
StringBuffer buffer=new StringBuffer(“abc”);

String s3=new String(buffer);//使用构造方法

String s4=buffer.toString();//使用toString()方法

@例题1:将数组拼接成一个字符串

int[] arr={44,55,66,77};
public static String arrayToString2(int[] arr){

StringBuffer sb=new StringBuffer();
sb.append("[");
//取出每一个字符后使用append方法拼接即可
sb.append("]");
return sb.toString;
}

@例题2:将字符串反转

String s=sc.nextLine();

//使用反转功能 
StringBuffer sb=new StringBuffer(s);
sb.reverse(); 
return sb.toStrin();//或
return new StringBuffer(s).reverse().toString();

//判断字符串是否对称

String s=sc.nextLine();
//将第1个与最后1个比较
//将第2个与倒数第2个比较
//...共需比较长度/2 次
public static boolean isSymmetry(String s){
//把字符串转成字符数组
char[] chs=s.toCharArray();
for(int start=0,end=chs.length-1;start<=end;start++,end--){
if(chs[start]!=chs[end]){
return false;
}
}
return true;
} 

//直接使用StringBuffer中的反转功能
public static boolean isSymmetry(String s){
return new StringBuffer(s).reverse().toString().equals(s);
}

2、StringBuilder类
1)不同步的,不安全的,是StringBuffer的简易替换,常用在单线程当中,其功能与构造方法完全与String相同
2)String/StringBuffer/StringBuilder的区别
①String是内容不可变的,后两个是可变的
②StringBuffer是同步的,数据安全但效率较低;StringBuilder则相反
3)StringBuffer与数组的区别
两者都可以看作为容器盛装其他的数据,但StringBuffer的数据最终是一个字符串数据,而数组可以放置多种数据,但必须是同一数据类型的
4)形参传递问题
String作为参数传递,效果和基本类型作为参数传递是一样的
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;
    sb2.append(sb1);
}

public static void change(String s1, String s2) {
    s1 = s2;
    s2 = s1 + s2;
}

}

2、数组的高级操作
1)排序
①冒泡排序:
相邻元素两两比较,大的往后放,第一次执行完毕,最大值将出现在最大索引处,之后以此类推
比较的次数是数组长度-1,代码如下

for(int x=0;x<arr.length-1;x++){
for(int y=0;y<arr.length-1-xly++){
if(arr[x]>arr[x+1]){
int temp=arr[x];
arr[x]=arr[x+1];
arr[x+1]=temp;
}
}
}

②选择排序
从索引为0处开始,依次与后面元素比较,小的往前放,一轮后最小的出现在最前面
这里写图片描述
代码如下:
for(int x=0;x

int max=arr.length-1;
int min=0
while(arr[mid]!=value){
if(arr[mid]>value){
max=mid-1;
else{
min=mid+!;
}
}
if(min>max){
return -1;
}
mid=(max+min)/2;
}

3、Arrays类
1)包含用来操作数组(比如排序和搜索)的各种方法
2)静态类
3)成员方法:
①public static String toString(基本数据类型数组);//把字符转为字符串
②public static void sort(基本数据类型数组);//对数组进行排序
③public static int binarySearch(基本数据类型数组,int key);//二分查找
4)只要是对象,就要判断是不是为空

4、基本类型包装类
1)为了对基本数据类型进行更多的操作,Java就针对每一种基本数据类型提供了对应的类类型,被称为包装类类型
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
2)常用的操作之一:用于基本数据类型与字符串之间的转换
3)基本类型和包装类之间的对应:Byte/Short/Integer/Float/Double/Character/Boolean
4)Integer类
①构造方法:public Integer(int value)
eg:Integer ii=new Integer(i);

String s=”100”;
Integer iii=new Integer(s);//实现了转换
System.out.println(iii);//100

public Integer(String s)

③Int类型与String类型的互相转换

//int转向String
int num=100;
//方式一:字符串的拼接
String s1=""+number;
//方式二:
String s2=String.valueOf(num);
//方式三:int -Integer-Stirng
 Integer i=new Integer(num);
 String s3=i.toString();
 //方式四:
 String s4=Integer.toString(num);

//String转向int
String s="100";
//方式一:String-Integer-int
Integer ii=new Integer(s);
int x=ii.intValue();
//方式二:调用成员函数
int y=Integer.parseInt(s);

④成员方法:
public int intValue();
public static int parselnt(String s)
public static String toString(int i)
public static Integer valueOf(int i)
public static Integer valueOf(String s)

5、jdk5的新特性
自动装箱:把基本类型转为包装类类型
自动拆箱:把包装类类型转为基本类型
仅在对象不为null时能生效

即系统允许做以下操作:

Integer i=100;//自动装箱
//规范做法应该是:Integer ii=new Integer(100);
i+=100;//先自动装箱,处理完后自动拆箱

6、缓冲池
1)看程序写结果:

Integer i1=128;
Integer i2=128;
System.out.println(i1==12);//false

Integer i1=127;
Integer i2=127;
System.out.println(i1==12);//true

针对byte范围内的数据,JVM制作了一个常量池。如果数据是该范围内的,每次并不创建新的空间。Integer的数据直接赋值,如果在-128~127之间,会直接从缓冲池中获取数据

7、Character类
1)提供了几种便利的方法(判断、转换大小写,数字)
2)成员方法大都是静态的

@例题四:如何校验一个qq号码

猜你喜欢

转载自blog.csdn.net/Tanqurey/article/details/81568020