常用类
一、字符串类
不可变字符串
String类:是一个不可变字符串类,调用任意方法不会改变原对象的值
创建方式:1.直接赋值字符串 2.new对象
//String的创建:
//1.直接赋值字符串
String a = "zs"; //存到常量池相同字符串常量只有一份
String b = "zs";
System.out.println(a==b); //true
//2.new对象方式
String aa = new String("zs");
String bb = new String("zs");
System.out.println(aa==bb); //false 每次new的地址不同
System.out.println(a==aa); //false 堆地址 vs 常量池地址
System.out.println(aa.equals(bb)); //true equals比较内容
//问题:String aa = new String("zs");创建了几个对象?
//如果程序中有“zs”,则创建一次(new 对象)
//如果程序中没有“zs”,则创建两次(new 对象,“zs”)
String的常用方法
//String字符串的常用方法: 调用方法不会改变原对象的值
String a = "hello";
String b = a.concat("world"); //字符串拼接,将两个字符串拼接到一起 等价+
System.out.println(b); //helloworld
System.out.println(a.charAt(1)); //根据下标获取对应字符 下标从0开始
System.out.println(a.contains("el")); //判断是否包含字串
char[] cs = a.toCharArray(); //将字符串转字符数组
System.out.println(Arrays.toString(cs));
System.out.println(a.indexOf("ll")); //匹配字串返回下标;匹配不上返回-1
System.out.println(" abc ddd ".trim()); //去除前后空格
System.out.println(a.toUpperCase()); //小写转大写
System.out.println("HEL".toLowerCase()); //大写转小写
System.out.println(a.endsWith("lo")); //是否以子串结尾
System.out.println(a.startsWith("el")); //是否以子串开头
//参数为正则表达式规则的字符串:
//正则表达式:代表特定规则含义的字符串序列 例如:按.拆分 .在正则表达式规则中表示任意字符
String[] ss = a.split("e"); //以字串进行拆分
System.out.println(Arrays.toString(ss));
String[] bb = "aa.bb.cc".split("[.]"); //.要通过[]转义
System.out.println(Arrays.toString(bb));
System.out.println(a.substring(1)); //ello 从当前下标处提取到最后
System.out.println(a.substring(1, 3)); //el 包含起始下标,不包含结束下标
//replace VS replaceAll
String name = "暴力的黄色小说,很暴力";
System.out.println(name.replace("暴力", "**"));
System.out.println(name.replaceAll("暴力", "**"));
//replace:完全匹配 replaceAll:可以完全也可以按规则匹配
//例如:把一串字符中的手机号替换成*** “答复13838383838对方13636363636应用”
//手机号规则:1开头 11位,纯数字
String c = "答复13838383838对方13636363636应用";
//System.out.println(c.replace("13838383838", "***"));
//[0-9]:0~9的一个数字 {10} 量词,正好10个
System.out.println(c.replaceAll("1[0-9]{10}", "***"));
可变字符串
概述:创建了一个可变字符串空间,空间里面的字符串内容可以随意改变
可变字符串:实例化该对象后,调用任意方法,可改变原对象的值
应用场景:如果操作字符串时,需要考虑优化性能,则可使用可变字符串
例如:循环进行字符串的拼接功能,使用String和可变字符串类测试,查看性能
String与StringBuilder性能测试
//StringBuilder/StringBuffer: 可变字符串,两者操作完全一样,实现的效果也一样
//区别在于安全性及性能的不同
//StringBuiler:不安全,性能高----在单线程中使用
//StringBuffer: 安全(加锁),性能低----在多线程中使用
public class Test1 {
public static void main(String[] args) {
StringBuilder x = new StringBuilder("aa");
x.append(666); //字符串追加 --可变字符串,调用方法,会改变原对象的值
System.out.println(x); //aa666
//String VS StringBuilder
//案例:循环1~10000的拼接,然后得到执行的时间
String a = "";
StringBuilder sb = new StringBuilder(""); //创建可变字符串缓冲区,里面给定空串
long start = System.currentTimeMillis(); //获取当前时间的毫秒值
for(int i=1;i<=10000;i++) {
//a += i; //看反编译工具 循环new了很多对象,再追加的
sb.append(i); //字符串追加
}
//当前毫秒值-之前的毫秒值= 执行的时间
System.out.println(System.currentTimeMillis()-start);
}
}
StringBuilder的其它方法调用
//StringBuilder的其他用法:
StringBuilder sb = new StringBuilder("helloworld");
//System.out.println(sb.reverse()); //字符串反转 dlrowolleh
System.out.println(sb.delete(3, 7)); //helrld 根据下标删除 包含起始下标,不包含结束下标
System.out.println(sb.toString()); //转字符串返回
二、BigDecimal类
比double存储的小数更精确
//案例: 执行0.01+0.09的操作
//double类型的数值存储,本身就是不精确的,看起来是0.01,存的值未必就是0.01
System.out.println(0.01+0.09); //0.09999999999999999
BigDecimal a = new BigDecimal(0.01);
//BigDecimal的0.01和0.09进行求和 0.099999999999999996877...
System.out.println(a.add(new BigDecimal(0.09)));
BigDecimal aa = new BigDecimal("0.01"); //参数必须为String类型
System.out.println(aa.add(new BigDecimal("0.09"))); //0.10 求和
System.out.println(aa.subtract(new BigDecimal("0.09"))); //-0.08 减
System.out.println(aa.multiply(new BigDecimal("0.9"))); //0.009 乘法
System.out.println(aa.divide(new BigDecimal("0.05"))); //0.2 除法
//ArithmeticException:当除不尽,则会出现算数异常;需要保留小数位
//参数1:BigDecimal对象 参数2:保留位数-int 参数3:四舍五入或其它模式-状态值int
//ROUND_HALF_UP:四十五入 BigDecimal.ROUND_FLOOR:截断多余位数
System.out.println(aa.divide(new BigDecimal("0.09"), 2, BigDecimal.ROUND_HALF_UP));
System.out.println(aa.divide(new BigDecimal("0.09"), 2, BigDecimal.ROUND_FLOOR));
//ROUND_UP:向上提升一位
System.out.println(aa.divide(new BigDecimal("0.09"), 2, BigDecimal.ROUN
三、随机类
Random类
public static void main(String[] args) {
//Random类:专门用于求随机数的类
//Random random = new Random(); //真随机
Random random = new Random(2); //伪随机:根据传入的种子数进行固定化随机
for(int i=0;i<5;i++) {
System.out.print(random.nextInt(5)+" "); //0~4随机
}
System.out.println("\n---------------------");
//案例:用方法封装,在一个范围内进行随机; 75-100
int val = getRandom(75,100);
System.out.println(val);
}
private static int getRandom(int start, int end) { //随机的方法实现
Random random = new Random();
return random.nextInt(end-start+1)+start;
}
Math类
Math类:是数学工具类,也可以进行随机操作
随机操作如下:
public static void main(String[] args) {
//Math类:数学类; 可以做随机操作,更多地可以做数学中的操作
//Math是工具类,里面提供的都是静态方法,直接使用类名调用即可
//Math.random(): 0.0~1.0之间的小数 包含0.0,不包含1.0
System.out.println(Math.random());
//案例:求1~1000以内的数
int random = (int)(Math.random()*1000)+1;
System.out.println(random);
//案例: 封装方法
System.out.println(getRandom(3,5));
}
private static int getRandom(int start, int end) {
int random = (int)(Math.random()*(end-start+1))+start;
return random;
}
数学操作:
//Math类做数学操作:
System.out.println(Math.abs(-1)); //求绝对值
System.out.println(Math.ceil(1.8)); //2.0 向上取整
System.out.println(Math.ceil(1.2)); //2.0
System.out.println(Math.floor(1.8)); //1.0 向下取整
System.out.println(Math.floor(1.2)); //1.0
System.out.println(Math.round(1.8)); //2 四舍五入
System.out.println(Math.round(1.2)); //1
四、日期类
Date类
直接实例化对象,即可获取当前日期的类,以格林威治时间格式显示
//Date类
Date date = new Date();
System.out.println(date);
//Date相关的get方法已经过时,已经被Calendar类所替代
//从1970年到现在的毫秒值
System.out.println(date.getTime()); //获取当前时间,long类型返回,单位毫秒
System.out.println(new Date(1000)); //1970+1毫秒
System.out.println(new Date(date.getTime())); //当前时间
SimpleDateFormat类
//SimpleDateFormat日期格式类: 可以将Date提供的日期,进行转格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//传入Date对象,返回日期的字符串格式
System.out.println(sdf.format(new Date())); //Date-->String
//已知字符串的日期,转Date对象
String a = "2020-03-05 08:09:30";
System.out.println(sdf.parse(a)); //String-->Date
Calendar类
//Calendar类:日历类(抽象类),可用于求日期的类,年,月,日等等
Calendar ca = Calendar.getInstance(); //通过静态方法返回子类对象
System.out.println(ca.get(Calendar.YEAR)); //获取年
System.out.println(ca.get(Calendar.MONTH)+1); //获取月(0~11)需+1
System.out.println(ca.get(Calendar.DATE)); //获取日
ca.set(Calendar.YEAR,2000); //年设置为2000
System.out.println(ca.get(Calendar.YEAR)); //2000
ca.add(Calendar.YEAR, 5); //在当前时间的基础上+数量
System.out.println(ca.get(Calendar.YEAR)); //2005
五、System类
系统类,提供了系统相关信息的工具类
//System类:代表系统属性信息的工具类
//System.arraycopy: 数组扩容
//System.currentTimeMillis(): 获取当前时间,返回毫秒值
//System.gc(); 手动垃圾回收
System.exit(0); //0正常退出 其他非正常退出
System.out.println("最后...");
六、集合概述
概述
与数组类似,用于存储多个数据的容器;
与数据的区别:
数组:类型相同,长度固定 集合:类型可以不同,长度不固定
数组可存基本类型或引用类型 集合只能存引用类型
数组不能调方法 集合是一个类,可调用方法操作元素
集合架构
集合是一个家族,最大的根接口为Collection;下面所有的集合类或接口都是它的子接口或实现类
Collection有两个重要的子接口: List,Set
List接口下面有两个重要的实现类:ArrayList,LinkedList
Set接口下面的两个重要的实现类:HashSet, TreeSet
List接口存储特点:存储元素有下标,有序,可重复
Set接口存储特点:存储元素无下标,无需,唯一
Collection接口
//Collection接口的特点:无下标
Collection col = new ArrayList();
col.add(1); //添加元素 自动装箱--Integer
col.add(3.5); //Double
col.add(2);
System.out.println(col); //[1, 3.5, 2]
Collection col2 = new ArrayList();
col2.add(6);
col.addAll(col2); //添加集合
System.out.println(col); //[1, 3.5, 2, 6]
col2.clear(); //清空元素
System.out.println(col2); //[]
System.out.println(col.contains(3.5)); //true 判断集合中是否包含指定元素
System.out.println(col.equals(1)); //false 判断集合元素是否相等
System.out.println(col.isEmpty()); //false 判断集合是否为空
System.out.println(col.remove(2)); //true 移除指定元素
System.out.println(col);
System.out.println(col.size()); //获取集合元素个数
Object[] o = col.toArray(); //集合转数组
System.out.println(Arrays.toString(o));
七、总结与作业
总结
作业
1、编写程序检查“hello” 是否包含e
2、编写程序去除“ ha he ”的前后空格
3、编写程序将 “hello” 全部变为大写,并输出到屏幕,截取子串”LLO” 并输出到屏幕
4、String s = "富家不用买良田,书中自有千钟粟。安居不用架高堂,书中自有黄金屋。出门莫恨无人随,书中车马多如簇。娶妻莫恨无良媒,书中自有颜如玉。男儿欲遂平生志,五经勤向窗前读。"
请分段输出诗句,按'。,'分段即可
提示:如果按照,和。拆分,需要写成[,。]
5、倒叙输出字符串“abcdefg”两种方式:StringBuilder, toCharArray
6、使用BigDecimal做加、减、乘、除计算
7、输入一个字符串,输入要查找的字符,求出查找特定字符出现的次数