2020-06-03 Javase 常用api

一、Arrays类
Arrays类是对数组进行操作的类,在java.util包下。

常用方法:

Arrays.toString( ):将数组转化成字符串
Arrays.sort( ):将数组升序排序,使用的是快速排序法
Arrays.binarySearch( ):(前提:数组升序)查找元素,若存在则返回位置索引;若不存在则返回(插入点索引的负值 - 1)
Arrays.equals( ):相等比较,比较两个数组的类型、个数以及值
Arrays.copyOf( ):数组的复制

二、String、StringBuffer以及StringBuilder
1.String类

说明

表示字符串,字符序列,不可变类
什么是不可变类?一旦字符串对象确定了,此字符串对象的字符序列就不能更改了。
区别下面三种情况

String str1 = "abc";//有对象,有字符序列

String str2 = "";//空串,有对象,没有字符序列

String str3 = null;//没对象

“==” 判断的是否为同一对象,同一内存地址

String s1 = "hello";
String s3 = new String("hello");
String s4 = "hello";
String s6 = new String("hello");
System.out.println(s1 == s3);//false
System.out.println(s1 == s4);//true
System.out.println(s3 == s6);//false

equals 比较的是字符序列是否相等

String s1 = "hello";
String s3 = new String("hello");
System.out.println(s1.equals(s3));//true,区分大小写
System.out.println(s1.equalsIgnoreCase(s3));//true,不区分大小写

字符串连接方法concat( )

String st1 = "hello";//方法区创建hello对象
st1 = st1.concat("tom");//方法区,创建了个新对象 hellotom,且st1此刻指向堆中的对象
求字符串的长度length( )

String st1 = "hello";
System.out.println(str11.length());

转换大小写

System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());

查找参数字符串在当前字符串中第一次出现的位置索引

String st22 = "hellohelloabc";
System.out.println(st22.indexOf("he"));//包含返回索引值
System.out.println(st22.indexOf("hex"));//不包含返回-1

查找参数字符串在当前字符串中最后一次出现的位置索引

System.out.println(st22.lastIndexOf("he"));//5

取出某一个位置索引处的字符

String st22 = "hellohelloabc";
System.out.println(st22.charAt(0));

substring( ):取子串

String st33 = "hellotomabc";
System.out.println(st33.substring(8));//从起始位置取到字符串末尾
System.out.println(st33.substring(8, 9));// [ 起始位置,终止位置 )

trim( )

String st44 = " h e l l o ";
System.out.println(st44.trim());// 去除前后空格

replaceAll( )

String st55 = "helloTom";
System.out.println(st55.replaceAll("hello", "你好"));//使用第二个参数字符串替换第一个参数字符串
System.out.println(st44.trim().replaceAll(" ", ""));//去掉字符串中所有空格

endWith( )

String st66 = "Demo.java";
System.out.println(st66.endsWith("java"));//是否以指定参数的字符串为结尾,输出:true

startsWith( )

System.out.println(st66.startsWith("Demo"));//是否以指定参数的字符串为开头,输出true

compareTo( ):比较字符串大小

String st77 = "abc";
System.out.println(st77.compareTo("xyz"));//返回负值,//比较的对象在参数对象之前,结果是负数。比较的是Unicode的码值
System.out.println(st77.compareTo("abc"));//相等返回0
System.out.println("xyz".compareTo("abc"));//返回正数,比较的对象在参数对象之后,结果是正数。比较的是Unicode的码值

toCharArray()

String st88 = "hello";
char[] crs = st88.toCharArray();//把字符串转换成字符数组 char[]

split( )

String st99 = "aa bb cc dd ee";
String[] strs = st99.split(" ");//把当前字符串用参数字符串分割成一个字符串数组

contains( )

System.out.println(st99.contains("aa")); //参数字符串在当前字符串中是否存在,存在true,不包含false

2.StringBuffer和StringBuilder
说明

可变字符串类;
字符序列频繁更改可以使用 StringBuffer是线程安全的,数据准确但速度慢
StringBuilder是线程非安全的,数据不准确但速度快 常用方法(二者差不多,以StringBuffer为例)

capacity():获取对象容量

StringBuffer sf = new StringBuffer();//默认16个字符大小的缓冲区
System.out.println(sf.);//16
StringBuffer ssf = new StringBuffer("hello");
System.out.println(ssf.capacity());//16 + 5 = 21
sf.append("hegfsgsgfgsgsfgsgfsgsdgsllo");
System.out.println(sf.capacity());//34,成倍扩容

trimToSize( ):

sf.trimToSize();//缩小容量为存储的字符大小
System.out.println(sf.capacity());//5

指定容量:

StringBuffer sf1 = new StringBuffer(100);//可以指定容量的大小

append( ):追加方法

sf1.append("hello");//追加
System.out.println(sf1);
char[] crs = {'a','b','c','d'};
sf1.append(crs,1,2);//执行追加方式
System.out.println(sf1);

insert( ):

sf1.insert(5,"tom");//向参数索引位置插入一个字符串
System.out.println(sf1);

setCharAt( ):

sf1.setCharAt(5, 'a');//修改某个索引位置的字符
System.out.println(sf1);

deleteCharAt( ):

sf1.deleteCharAt(5);//删除索引位置的字符
System.out.println(sf1);
sf1.delete(4, 6);//范围删除:左闭右开
System.out.println(sf1);

reverse( ):

StringBuffer sr = new StringBuffer();
sr.append("123");
System.out.println(sr);
sr.reverse();//返转
System.out.println(sr);

toString( ):

String str = sr.toString();//将StringBuffer类型转换成String类型

补充知识点:

Arrays.sort(arr, 0, 3); //对指定范围内的元素进行排序,左闭右开
Arrays.binarySearch(arr, 0, 3, 45); //在指定范围内进行查找,左开右闭
Arrays.copyOfRange(arr, 1, 3); //指定复制范围,左闭右开
int [ ] arr = {11,22,33,44};
Arrays.fill(arr, 222); //把所有元素进行替换,结果:{222,222,222,222}
Arrays.fill(arr, 0, 2, 888); //指定范围进行填充,左闭右开,结果:{888,888,33,44}
对应编译器能够确定的常量值,都是在常量池中维护。常量池的好处,就是重复使用已经存在的常量值对象,避免频繁的创建对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值