Day 12
2019年5月1日。
这是我学习Java的第十二天。
这一天,我学到了以下的知识。
排序算法的补充
-
基数排序
原理:分配再排序,不需要让元素之间进行比较并调换位置(缺点:无法比较负数)代码如下
public class 基数排序算法 {
public static void main(String[] args) {
int[] arr = {1, 9, 8, 12, 23, 28, 32, 42, 102, 100, 259};
sortArray(arr);
System.out.println(Arrays.toString(arr));
}
private static void sortArray(int[] arr) {
//定义一个二维数组
int[][] tempArr = new int[10][arr.length];//里面的一维数组的长度最大就和待排序的数组长度一样
//定义一个统计的一维数组,可以统计二维数组每个桶中放了多少数字
int[] counts = new int[10];//长度和二维数组长度保持一样
//获取数组中最大的数
int max = getMax(arr);
//计算数组中最大的数的有几位,这就是我们需要比较的轮次
int len = String.valueOf(max).length();
for (int i = 0, n = 1; i < len; i++, n *= 10) {
for (int j = 0; j < arr.length; j++) {
//获取每个位上的数字
int ys = arr[j] / n % 10;
//我们找一个二维数组,数组中放10个桶,这个桶就是一位数组
// counts[ys]++ 意思是,二维数组的桶中放一个数字,那么统计数组对于的位置就统计一下
tempArr[ys][counts[ys]++] = arr[j];
}
//我们遍历统计数组,取出二维数组中的元素,放回原数组
int index = 0;
for (int k = 0; k < counts.length; k++) {
if (counts[k] != 0) {
for (int h = 0; h < counts[k]; h++) {
arr[index] = tempArr[k][h];
index++;
}
//情况一轮过后统计的个数
counts[k] = 0;
}
}
}
}
private static int getMax(int[] arr) {
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
- 二分查找
原理:如下图所示(缺点:数组必须有序)
代码如下
public class ArrayDemo2 {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
//int index=findIndex(arr,40);
//System.out.println(index);
//二分查找的前提:是数组元素必须有序
int index = selectIndex(arr, 80);
System.out.println("索引是" + index);
}
private static int selectIndex(int[] arr, int ele) {
//定义三个索引
int minIndex = 0;
int maxIndex = arr.length - 1;
int centerIndex = (minIndex + maxIndex) / 2;
while (minIndex <= maxIndex) {
if (ele == arr[centerIndex]) {
return centerIndex;
} else if (ele < arr[centerIndex]) {
maxIndex = centerIndex - 1;
} else if (ele > arr[centerIndex]) {
minIndex = centerIndex + 1;
}
//重新计算中间索引
centerIndex = (minIndex + maxIndex) / 2;
}
return -1;
}
private static int findIndex(int[] arr, int ele) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == ele) {
return i;
}
}
return -1;
}
}
Arrays类
Arrays,是针对数据进行操作的工具类,提供了排序,查找等功能。
常用的成员方法
- public static String toString(int[] a)
:按照标准格式输出整型数组
- public static void sort(int[] a)
:对整型数组进行快速排序
- public static int binarySearch(int[] a,int key)
:对整型数组进行二分查找
- static boolean equals(int[] a, int[] a2)
:比较两个整形数组中的元素
- static void fill(int[] a, int val)
:分配元素到整型数组中
包装类型
为了对基本数据类型进行更多的操作和更方便的操作,java就针对每一种基本数据类型提供了对应的类类型,用于基本数据类型与字符串之间的转换。
基本数据类型 | 包装类型 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
其中,以最常见的Integer类进行讲解。
Integer类
构造方法:public Integer(int value) // 构造一个值为value的Integer对象
public Integer(String s) // 构造一个值为s的Integer对象
String和int类型的相互转换
- int – String
- 和""进行拼接
- public static String valueOf(int i)
- int – Integer – String
- public static String toString(int i) - String – int
- String – Integer – intValue();
- public static int parseInt(String s)
正则表达式
正则表达式,是正确规则的表达式,是一个独立的语法,很多的编程语言都支持,其用来检验一段数据是否符合定义的规则。
组成规则
- 字符
- x 字符 x。举例:‘a’表示字符a
- \ 反斜线字符
- \n 新行(换行)符 (’\u000A’)
- \r 回车符 (’\u000D’) - 字符类
- [abc] a、b 或 c(简单类)
- [^abc] 任何字符,除了 a、b 或 c(否定)
- [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
- [0-9] 0到9的字符都包括 - 预定义字符类
- . 任何字符。若就是.字符本身,则为.
- \d 数字:[0-9]
- \w 单词字符:[a-zA-Z_0-9] - 边界匹配器
- ^ 行的开头
- $ 行的结尾
- \b 单词边界(就是不是单词字符的地方) - Greedy 数量词
- X? X,一次或一次也没有 比如""空串 就是没有
- X* X,零次或多次 大于等于1次 都算多次
- X+ X,一次或多次
- X{n} X,恰好 n 次
- X{n,} X,至少 n 次
- X{n,m} X,至少 n 次,但是不超过 m 次
正则表达式的匹配功能
public boolean matches(String regex) // :String类的功能
正则表达式的分割功能
public String[] split(String regex)) // :String类的功能
正则表达式的替换功能
public String replaceAll(String regex,String replacement) // :String类的功能
模式器Pattern类、匹配器Matcher类
- Pattern:模式器,可以封装一个正则表达式
- Matcher:匹配器
- find():尝试查找与该模式匹配的输入序列
- group():返回由以前匹配操作所匹配到的输入序列
- 注意:一定要先使用find()方法先找到,才能用group()方法获取出来
Math类
Math,包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
常用的成员变量
- public static final double E
:自然底数
- public static final double PI
:圆周率
常用的成员方法
- 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)
:获取最大值
- public static int min(int a, int b)
:获取最小值
- public static double pow(double a,double b)
:获取a的b次幂
- public static double random()
:获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0
- public static int round(float a)
:四舍五入
- public static double sqrt(double a)
:获取正平方根