String&包装类&位运算&Math类
1 String
1.1 replaceAll方法
public class StringDemo {
public static void main(String[] args) {
practice();
demo1();
}
private static void practice() {
// 计算字符串的平均碎片长度
// 对于任意一个字符串,都可以看作是由一组叠字和单字组成的集合
// aaabbcaabbcc aaa bb c aa bb cc = 12 / 6 = 2
String str = "aaabbcaabbcc";
// 获取字符串的总长度
int length = str.length();
// 获取碎片的个数
String str1 = str.replaceAll("(.)\\1+", "$1");
double count = str1.length();
// 计算平均碎片长度
System.out.println(length / count);
}
private static void demo2() {
// 交换字符串的内容
String str = "张三 李四 王五 周六";
System.out.println(str.replaceAll("(.*)(李四)(.*)(周六)","$1$4$3$2"));
// 把字符串中的叠字替换成单字
// 我我我我我我我爱爱爱爱爱爱学学学学学画画
// 我爱学画画
String str1 = "我我我我我我我爱爱爱爱爱爱学学学学学画画";
System.out.println(str1.replaceAll("(.)\\1+","$1"));
}
private static void practice3() {
// 输入一个字符串,统计每一个字符出现的次数 aaabbbccca
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
// 如果字符串中还有内容,就继续替换
while (str.length() != 0){
// 替换字符串中的内容
// 替换之前记录下字符串的长度
int len = str.length();
// 获取字符串第一个字符
char c = str.charAt(0);
str = str.replaceAll(c+"","");
System.out.println("字符" + c + "出现的次数是" + (len - str.length()));
}
}
private static void practice2() {
// 输入一个字符串,提取其中所有的数字并排序
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
// 提取数字
str = str.replaceAll("\\D", "");
System.out.println(str);
char[] chars = str.toCharArray();
Arrays.sort(chars);
System.out.println(chars);
}
private static void practice1() {
// 匹配邮箱
// @qq.com @163.com @sina.cn @cfe.com.cn
String mail = "fengshuo@qq.com";
System.out.println(mail.matches("\\w{6,32}@\\w+(\\.com)") || mail.matches("\\w{6,32}@\\w+(\\.com)?(\\.cn)"));
}
private static void demo1() {
String str = "adf9fef8gvrg76r3g";
// 把a替换成c
System.out.println(str.replace('a','c'));
// 将所有的数字替换成 -
System.out.println(str.replaceAll("\\d","-"));
// 提取这个字符串中所有的数字
// System.out.println(str.replaceAll("[^0-9]",""));
System.out.println(str.replaceAll("\\D",""));
}
}
1.2 spilt方法
private static void demo3() {
String str = "1,2,3,4";
String[] split = str.split(",");
System.out.println(Arrays.toString(split));
}
// split方法中也可以使用正则表达式
String str = "fe8fe9fr8gr";
String[] split = str.split("\\d");
System.out.println(Arrays.toString(split));
// 生成6位字母和数字组成的验证码
String code = UUID.randomUUID().toString().substring(0, 6);
System.out.println(code);
}
2 包装类
byte | short | int | long | float | double | char | boolean | void |
---|---|---|---|---|---|---|---|---|
Byte | Short | Integer | Long | Float | Double | Character | Boolean | Void |
上面一行全部都是基本数据类型,下面一行全部都是引用数据类型
2.1 Void类(了解)
public static Void demo1(){
// Void类是一个最终类,并且构造方法是私有的
return null;
}
2.2 数值类型(掌握)
装箱:利用基本数据类型的变量来创建引用数据类型
// 装箱
int i = 10;
Integer integer = new Integer(i);
JDK1.5后可以使用自动装箱
自动装箱:系统自动会把基本数据类型的变量创建成引用数据类型
// 自动装箱 底层会自动调用对应类身上的valueOf方法
// Integer integer = Integer.valueOf(i);
Integer integer = 10;
Double d = 3.5;// Double.valueOf(3.5);
Character c = 'a'; // Character.valueOf('a')
自动装箱的特点
// Byte、Short、Integer、Long类型在-128到127之间都是返回同一个对象
// Float、Double是一直创建新的对象
// Character类型是0到127之间是同一个对象
// Boolean类型只有两个对象 TRUE FALSE
拆箱:把引用数据类型转换成基本数据类型
自动拆箱:自动把引用数据类型转换成基本数据类型
// Integer integer = new Integer(3);
// int i = integer.intValue();
// 自动拆箱调用的是XXXValue方法
Integer integer = new Integer(3);
int i = integer;
Double dou = new Double(3.28);
double d = dou;
Character ch = new Character('a');
char c = ch;
// 当引用类型和对应的基本类型进行运算的时候,引用类型会自动拆箱
Integer in = -1000;
int i1 = -1000;
System.out.println(in == i1);
2.3 Integer的构造方法
// NumberFormatException:数字格式异常
// 字符串中第一个字符的符号只能是 + 或者 -
Integer integer = new Integer("-100");
System.out.println(integer);
2.4 常用方法
hashCode()
包装类产生的对象的哈希码值是固定不变的。
Byte、Short、Integer、char的哈希码值都是自身的值
Long、Float、Double都是固定值
Boolean如果是true返回1231,如果是false返回1237
public static void main(String[] args) {
// java.lang包中的内容都不需要import引入,默认系统已经引入了
Short s = 200;
System.out.println(s.hashCode());
// NaN not a number 非数字
// 抛出异常 ArithmeticException: / by zero
// double d = 1 / 0;
double d = 1.0 / 0; // Infinity 正无穷
System.out.println(d);
double d1 = -1.0 / 0;
System.out.println(d1);//-Infinity 负无穷
double d2 = 0.0 / 0;
System.out.println(d2);// NaN 非数字
// 非数字和自身比较时都不相等
System.out.println(d2 == (0.0 / 0));
// 判断非数字
System.out.println(Double.isNaN(0.0 / 0));
Double aDouble = new Double(0.0 / 0);
System.out.println(aDouble.isNaN());
// 求和
System.out.println(Double.sum(3.14, 2.11));
// 最大值
System.out.println(Integer.max(3,5));
// 最小值
System.out.println(Integer.min(3,5));
// 将十进制转换成二进制
System.out.println(Integer.toBinaryString(10));
// 将十进制转换成八进制
System.out.println(Integer.toOctalString(9));
}
3 位运算
3.1 位运算介绍
位运算是针对于数据的二进制的补码形式进行运算的。
常用运算:
& 与
| 或
^ 异或
<< 左移
>> 右移
>>> 无符号右移
~ 取反
位运算一般用于整数运算
3.2 进制转换
十进制转换成二进制:不断除以2取余,然后将余数倒着排序。
二进制转换成十进制:从低位次起,每位按位次乘以2的位次的次幂。然后求和
1101 = 1 * 2 ^ 0 + 0 * 2 ^ 1 + 1 * 2 ^ 2 + 1 * 2 ^ 3 = 1 + 0 + 4 + 8 = 13
11110 = 0 + 2 + 4 + 8 + 16 = 30
十进制向哪个进制转换,就除以哪个数字,然后将余数倒排
二进制转八进制: 从低位次起,每三位分为一组,产生一个八进制的数字,最高位不足三个,补0,三位以内,按照二进制向十进制的转换规则进行运算,产生的八进制数字按顺序排列。
010011101 = 0235
八进制转二进制:每一位八进制转换成三位二进制数字,然后将这个数字按顺序排列
0235 = 010011101
二进制转十六进制: 和二进制转八进制差不多,只是把三位一组变成分为四位一组
110111011 = 0x1bb
十六进制转换二进制:和八进制转二进制相似,一位变成四位
3.3 原码反码补码
所有的数据在底层都是以二进制数据的补码形式存储的。
对于正数来说,原码反码和补码是一样的。
符号位如果是正数就是0,如果是负数就是1
3.4 位运算
运算的前提条件是要把数据转换成二进制数据的补码形式
-
按位与 : & 两个操作数中都是1,结果是1,否则结果就是0
- 任意一个数&一个偶数,结果一定是偶数
- 任意一个数&1,如果结果是0,就是偶数
-
按位或: | 两个位只要有一个为1,那么结果就是1,否则是0
- 任意一个数 | 0 , 结果还是自身
- 任意一个数 | 一个奇数,结果一定是奇数
-
按位异或: ^ 两个操作数中,相同就是0,不相同就是1
- 任何数异或自己都是0,异或0都是自己
-
左移: << 左移n位就是乘以2的n次方。 向左移动后,左边舍弃,右边补0
-
右移: >> 正数右移n位就是除以2的n次方。右边舍弃,左边补位(正数补0,负数补1).负数最右移动到-1 ,正数最大移动到0
-5 >> 2
原码: 10000000 00000000 00000000 00000101
反码: 11111111 11111111 11111111 11111010
补码: 11111111 11111111 11111111 11111011
右移: 1111111111 11111111 11111111 111110
反码: 11111111111111111111111111111111101
原码: 1000000000000000000000000000000010
- 无符号右移: >>> 有右移类似,但是最高位空出之后,无论正数还是负数,一律补0
- 取反 : ~ 将数字转换成二进制之后,1变0,0变1,然后最高位如果是1,按照-128,其余位按照二进制转十进制的规则计算,然后相加。如果最高位是0,那么不用计算,忽略最高位
取反规律: ~i = -i - 1
- 两个变量数据交换
int a =10;
int b = 20;
// 交换方式一
int temp = a;
a = b;
b =temp;
// 交换方式二
a = b - a;
b = b - a;// b = b - (b - a) b = a
a = b + a;// a = a + b - a a = b
// 交换方式三 (任何数异或自己都是0,异或0都是自己)
a = a ^ b;
b = a ^ b;// b = a ^ b ^ b b = a ^ 0 b = a
a = a ^ b;// a = a ^ b ^ a a = b
System.out.println(a);
System.out.println(b);
4 Math类
Math类是一个最终类,里面包含了很多常用的和数学计算相关的方法。
public static void main(String[] args) {
// 自然常数
System.out.println(Math.E);
// 圆周率
System.out.println(Math.PI);
// 绝对值
System.out.println(Math.abs(-8));
//获取立方根
System.out.println(Math.cbrt(64));
// 向上取整
System.out.println(Math.ceil(-3.1));
// 向下取整
System.out.println(Math.floor(-7.89));
// 平方根
System.out.println(Math.sqrt(9));
// 四舍五入
System.out.println(Math.round(3.5));
// 求最大值
System.out.println(Math.max(2,6));
// 求最小值
System.out.println(Math.min(2,6));
// 加权函数
// Math(a,b) a的b次方
System.out.println(Math.pow(3,3));
// 获取[0,1)随机数
System.out.println(Math.random());
// 随机生成6位数字验证码 [100000,1000000)
int code = (int)(Math.random() * (1000000 - 100000) + 100000);
System.out.println(code);
}