数组高级以及Arrays
- 排序
- 冒泡排序
- 选择排序
- 查找
- 基本查找(数组无序)
- 二分查找(数组有序,千万不要先排序再查找)
- Arrays工具类
- 是针对数组进行操作的工具类,包括排序和查找
- 需要掌握的方法:
- 把数组转成字符串
- 排序
- 二分查找
- Arrays工具类的源码解析
- 把字符串中的字符进行排序
Integer
概述
- 为了让基本类型的数据有更多的操作,为每种基本类型提供了对应的包装类型
| 基本类型 | 包装类型 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
构造方法
- Integer i = new Integer(100);
- Integer ii = new Integer("1000");//注意字符串必须由数字字符组成
String和int的相互转换
- String ---- int
- 推荐 Integer.parseInt("1000");
- int ---- String
- 推荐 String.toString(1000);
//int --- String
int num = 100;
// 方式1:
String s = "" + num;
//方式2:valueOf
String s1 = String.valueOf(num);
//方式3:toString()
String s3 = Integer.toString(num);
// String --- int
String ss = "100";
int i = Integer.parseInt(ss);
进制间的转换
- 进制的范围:2-36
// 常用进制之间的转换
public static String toBinaryString(int i));
public static String toOctalString(int i);
public static String toHexString(int i);
//十进制到其他进制
public static String toString(int i,int radix);
//其他进制到十进制
public static int parseInt(String s,int radix);
JDK5的新特性
- 自动装箱
- 自动拆箱
- 理解下面的代码
Integer i= 100;
i += 200;
// 自动装箱,自动拆箱
Integer ii = 100;
ii += 20;
System.out.println("ii:" + ii);
// 反编译过程
// Integer ii = Integer.valueOf(100);//自动装箱
// ii = Integer.valueOf(ii.intValue() + 20);//自动拆箱,自动装箱
// System.out.println((new StringBuffer("ii:")).append(ii).toString());
// 注意,如果Integer ii = null;会报错
面试题
- -128到127之间的数据缓冲池
// 缓冲池
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2); //true
System.out.println(i1.equals(i2)); //true
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4); //false
System.out.println(i3.equals(i4)); //true
Character
构造方法
Character ch = new Character('a')
要掌握的方法
- 判断给定的字符是否大写:
public static boolean isUpperCase(char ch) - 判断给定的字符是否小写:
public static boolean isLowerCase(char ch) - 判断给定的字符是否是数字字符:
public static boolean isDigit(char ch) - 把给定的字符转成大写:
public static char toUpperCase(char ch) - 把给定的字符转成小写:
public static char toLowerCase(char ch)
案例
- 统计字符串中大写,小写及数字字符出现的次数
正则表达式
概述
符合一定规则的字符串
常用规则
- '\r'是回车,'\n'是换行,前者使光标到行首,后者使光标下移一格。通常用的Enter是两个加起来。
常见功能
- 判断
- String类的
public boolean matches(String regex)
- String类的
- 分割
- String类的
public String[] split(String regex)
- String类的
- 替换
- String类的
public String replaceAll(String regex,String replacement)
- String类的
- 获取
- Pattern和Matcher
String s1 = "jin tian hao kai xin wei sha ne bu xiao de";
//定义规则
String regex = "\\b\\w{3}\\b";
//把规则编译成模式对象p
Pattern p = Pattern.compile(regex);
//通过模式对象得到匹配器对象m
Matcher m = p.matcher(s1);
//调用匹配器对象m的功能
while(m.find()) {
System.out.println(m.group());//两个方法必须配套使用
}
案例
- 判断电话号码和邮箱
- 按照不同的规则分割数据
- 把论坛中的数据替换为*
- 获取字符串中由3个字母组成的单词
/*
* 字符串“99 76 87 23 45 78”
* 输出排好序的字符串“23 45 76 78 87 99”
* 分析:
* 定义一个字符串
* 对字符串分割,得到字符串数组
* 把字符串数组转成int数组
* 对int数组排序
* 把排好序的数组转成字符串
* 输出字符串
* */
String s = "99 76 87 23 45 78";
String[] str = s.split(" ");
// 将字符串数组转换为int数组
int[] arr = new int[str.length];
for(int i = 0;i<arr.length;i++) {
arr[i] = Integer.parseInt(str[i]);
}
Arrays.sort(arr);
// 将int数组转换为字符串
StringBuilder sb = new StringBuilder();
for(int i = 0;i<arr.length;i++) {
sb.append(arr[i]).append(" ");
}
String result = sb.toString().trim();
System.out.println(result);
Math
概述
针对数学运算进行操作的类
常用方法
- 绝对值:
public static int abs(int a) - 向上取整:
public static double ceil(double a) - 向下取整:
public static double floor(double a) - 两个数据中的较大值
public static int max(int a, int b) - a的b次幂
public static double pow(double a,double b) - 随机数:
public static double random();//[0.0,1.0)左闭右开 - 四舍五入:
public static int round(float a)- 源码:
(int) Math.floor(a + 0.5f)
- 源码:
- 正平方根:
public static sqrt(double a)
案例
- 猜数字小游戏
- 获取任意范围的随机数
Random
概述
用于产生随机数的类
构造方法
- Random():默认种子,每次产生的随机数不同
- Random(long seed):给定种子,相同种子产生相同随机数
成员方法
- int nextInt():返回int范围内的随机数d的shu'ju
- int nextInt(int n):返回[0,n)范围的随机数
System
概述
系统类,提供一些有用的字段和方法
成员方法
- 运行垃圾回收器:
public static void gc()- 执行System.gc()之前,系统会自动调用finalize()方法清除对象占用的资源,并通过super.finalize()方式实现从下到上的finalize()方法的调用,即先释放自己的资源,再去释放父类的资源。
- 退出jvm:
public static void exit(int status);//参数为状态码,非0表示异常终止- 经常会使用System.exit(1) 或 System.exit(0)
- 获取当前时间的毫秒值:
public static long currentTimeMillis(); - 数组复制:
public static void arraycopy(object src,int srcPos,object dest,int destPos,int length);- 从指定数组中复制一段数据,从源数组指定位置开始,复制到目标数组的指定位置,指定长度
- 参数:源数组 源数组的起始位置 目标数组 目标数组的起始位置 长度
BigInteger
概述
针对大整数的运算(超过Integer范围的数据)
构造方法
- BigInteger(String s):保证数据精确,其他不能保证
成员方法
- 加:
public BigInteger add(BigInteger val); - 减:
public BigInteger subtract(BigInteger val); - 乘:
public BigInteger multiply(BigInteger val); - 除:
public BigInteger divide(BigInteger val);//返回结果,舍弃小数 - 商和余数:
public BigInteger[] divideAndRemainder(BigInteger val);//返回一个长度为2的数组,一个是商,一个是余数
//加法运算
BigInteger sum = b1.add(b2);
System.out.println("两个整数的和是:"+ sum);
//减法运算
BigInteger sub = b2.subtract(b1);
System.out.println("两个整数的差是:" + sub);
//乘法运算
BigInteger multiply = b1.multiply(b2);
System.out.println("两个整数相乘是:" + multiply);
//除法运算 BigInteger divide = b2.divide(b1);
System.out.println("两个整数的商是:" + divide);
BigDecimal
概述
- 浮点数据做运算会丢失精度(float类型的数据存储会带有效数字位),金融方面用的比较多
构造方法
- BigDecimal(String s)
成员方法
- 加
public BigDecimal add(BigDecimal augend) - 减
public BigDecimal subtract(BigDecimal augend) - 乘
public BigDecimal multiply(BigDecimal augend) - 除
public BigDecimal divide(BigDecimal augend) - 自己保留小数几位
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)- 指定精度,指定舍入模式
Date/DateFormat
概述
- Date表示日期类,可以精确到毫秒
- DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,使用其子类SimpleDateFormat
Date
- 构造方法
- Date():根据当前的默认毫秒值创建日期对象
- Date(long time):根据给定的毫秒值创建日期对象
- 成员方法
- public long getTime();获取时间,以毫秒为单位
- public void setTime(long time):设置时间
- 日期和毫秒值之间的转换
- getTime()
- 构造方法&setTime(long time)
- 你来到这个世界多少天了
DateFormat
- SimpleDateFormat(String pattern):给定模式
- yyyy-mm-dd HH:mm:ss
- 日期和字符串的转换
- Date ----String
public final String format(Date date)
- String ---Date
public Date parse(String source)
- Date ----String
- 案例
- 制作一个针对日期操作的工具类
// 相当于借助SimpleDateFormat这个中介,说明需要转换成什么模式的字符串,或在解析之前说明字符串的模式
// Date ---- String
Date d = new Date();
//给定模式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
// String ---- Date
String s1 = "2013-10-01 00:00:00";
// 模式
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf1.parse(s1);
System.out.println(d1);
Calendar
概述
日历类,封装了所有的日历字段值,通过统一的方法根据传入的不同日历字段可以获取值
得到一个日历对象
Calendar rightNow = Calendar.getInstance(); //本质返回的是子类对象
成员方法
- 根据日历字段得到对应的值
- 例如
get(Calendar.YEAR)
- 例如
- 根据日历字段和一个正负数确定是添加还是减去对应日历字段的值
- 例如
add(Calendar.YEAR, -5)
- 例如
- 直接设置日历对象的年月日
- 例如
set(2011,11,11)//12月11日,月份从0开始
- 例如
//日历字段由当前日期初始化
Calendar rightNow = Calendar.getInstance();
// 获取年
int year = rightNow.get(Calendar.YEAR);
int month = rightNow.get(Calendar.MONTH)+1;
int day = rightNow.get(Calendar.DAY_OF_MONTH);
System.out.println(year);
System.out.println(month);
System.out.println(day);
案例
- 计算任意一年的2月份有多少天?
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int year = sc.nextInt();
Calendar c = Calendar.getInstance();
c.set(year, 2,1); //实际是3月1日
c.add(Calendar.DATE, -1);
System.out.println(c.get(Calendar.DATE));