4.7 Array的高级操作
运行结果:
运行结果:
运行结果:
运行结果:
运行结果:
运行结果:
运行结果:
运行结果:
运行结果:
运行结果:
运行结果:
4.7.1 排序
1、冒泡排序
相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。
import java.awt.image.BufferStrategy;
public class ArrayDemo2 {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);
bubbleSort(arr);
System.out.println("排序后:");
printArray(arr);
}
// 冒泡排序
public static void bubbleSort(int[] arr){
for(int x = 0;x<arr.length-1;x++){
for(int y = 0;y<arr.length-1-x;y++){
if(arr[y]>arr[y+1]){
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
//遍历功能
public static void printArray(int[] arr){
System.out.print("[");
for(int x=0;x<arr.length;x++){
if(x==arr.length-1){
System.out.print(arr[x]);
}
else{
System.out.print(arr[x]+" ");
}
}
System.out.println("]");
}
}
运行结果:
2、选择排序
从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处。
public class ArrayDemo2 {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);
selectSort(arr);
System.out.println("排序后:");
printArray(arr);
}
// 选择排序
public static void selectSort(int[] arr) {
for (int x = 0; x < arr.length - 1; x++) {
for (int y = x; y < arr.length - 1; y++) {
if (arr[x] > arr[y + 1]) {
int temp = arr[x];
arr[x] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
}
// 遍历功能
public static void printArray(int[] arr) {
System.out.print("[");
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
System.out.print(arr[x]);
} else {
System.out.print(arr[x] + ", ");
}
}
System.out.println("]");
}
}
运行结果:
练习:把字符串中的字符进行排序。"dacgebf"-->"abcdefg"
public class ArrayTest {
public static void main(String[] args) {
String s = "dacgebf";
char[] chs = s.toCharArray();
// bubbleSort(chs);
selectSort(chs);
s = String.valueOf(chs);
System.out.println(s);
}
// 冒泡排序
public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
// 选择排序
public static void selectSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = x; y < chs.length - 1; y++) {
if (chs[x] > chs[y + 1]) {
char temp = chs[x];
chs[x] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
}
运行结果:
4.7.2二分 查找(针对有序数组)
public class ArrayDemo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {11,22,33,44,55,66,77};
//写功能实现
int index = getIndex(arr, 33);
System.out.println("index:"+index);
//假如这个元素不存在后有什么现象呢?
index = getIndex(arr, 333);
System.out.println("index:"+index);
}
/*
* 两个明确:
* 返回值类型:int
* 参数列表:int[] arr,int value
*/
public static int getIndex(int[] arr,int value){
//定义最大索引,最小索引
int max = arr.length -1;
int min = 0;
//计算出中间索引
int mid = (max +min)/2;
//拿中间索引的值和要查找的值进行比较
while(arr[mid] != value){
if(arr[mid]>value){
max = mid - 1;
}else if(arr[mid]<value){
min = mid + 1;
}
//加入判断
if(min > max){
return -1;
}
mid = (max +min)/2;
}
return mid;
}
}
运行结果:
4.7.3 Arrays
此类包含用来操作数组(比如排序和搜索)的各种方法。
/*
* Arrays:针对数组进行操作的工具类。比如说排序和查找。
* 1:public static String toString(int[] a) 把数组转成字符串
* 2:public static void sort(int[] a) 对数组进行排序
* 3:public static int binarySearch(int[] a,int key) 二分查找
*/
public class ArraysDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
// public static String toString(int[] a) 把数组转成字符串
System.out.println("排序前:" + Arrays.toString(arr));
// public static void sort(int[] a) 对数组进行排序
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr));
// public static int binarySearch(int[] a,int key) 二分查找
// [13, 24, 57, 69, 80]
System.out.println("binarySearch:"+Arrays.binarySearch(arr, 80));
}
}
运行结果:
还有一个将数组转成List集合的方法:
public static <T> List<T> asList(T... a):返回一个受指定数组支持的固定大小的列表。
4.8 正则表达式
正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。
4.8.1 正则表达式的规则
4.8.2 正则表达式判断功能
String类中的public boolean matches(String regex)方法
import java.util.Scanner;
/*
* 校验qq号码:
* 要求1:5-15位数字
* 要求2:0不能开头
* 分析:
* 1、键盘录入qq号
* 2、写一个功能实现校验
* 3、调用功能,输出结果
*/
public class RegexDemo2 {
public static void main(String[] args) {
// 创建键盘对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的QQ:");
String qq = sc.nextLine();
System.out.println(checkQQ(qq));
}
public static boolean checkQQ(String qq) {
// String regex ="[1-9][0-9]{4,14}";
// //public boolean matches(String regex)告知此字符串是否匹配给定的正则表达式
// boolean flag = qq.matches(regex);
// return flag;
// return qq.matches("[1-9][0-9]{4,14}");
return qq.matches("[1-9]\\d{4,14}");
}
}
运行结果:
练习1:判断手机号码是否符合规则
/*
* 判断功能
* String类的public boolean matches(String regex)
*
* 需求:
* 判断手机号码是否满足要求?
*
* 分析:
* A:键盘录入手机号码
* B:定义手机号码的规则
* 13436975980
* 13688886868
* 13866668888
* 13456789012
* 13123456789
* 18912345678
* 18886867878
* 18638833883
* C:调用功能,判断即可
* D:输出结果
*
* 规则:11位数字
* 以1开头
* 第二位是3或者8
*/
public class RegexDemo {
public static void main(String[] args) {
//键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入要判断的手机号码:");
String phoneNumber = sc.nextLine();
//定义手机号码规则
String regex = "1[38]\\d{9}";
//调用规则,判断即可
boolean flag = phoneNumber.matches(regex);
System.out.println("flag:"+flag);
}
}
运行结果:
练习2:校验邮箱
/*
* 校验邮箱
*
* 分析:
* A:键盘录入邮箱
* B:定义邮箱的规则
* 1517806580@qq.com
* liuyi@163.com
* linqingxia@126.com
* fengqingyang@sina.com.cn
* fqy@itcast.cn
* C:调用功能,判断即可
* D:输出结果
*/
public class RegexTest {
public static void main(String[] args) {
//键盘录入邮箱
Scanner sc = new Scanner(System.in);
System.out.println("请输入邮箱:");
String email = sc.nextLine();
//定义邮箱规则
//String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";
//调用功能
boolean flag = email.matches(regex);
System.out.println("flag:"+flag);
}
}
运行结果:
4.8.3 正则表达式的分割功能
String类中的public String[] split(String regex)方法
import java.util.Scanner;
/*
* 分割功能
* String类的public String[] split(String regex)
* 根据给定正则表达式的匹配拆分此字符串。
*
* 举例:
* 百合网,世纪佳缘,珍爱网,QQ
* 搜索好友
* 性别:女
* 范围:"18-24"
*
* age>=18 && age<=24
*/
public class RegexDemo {
public static void main(String[] args) {
//定义一个年龄搜索范围
String ages = "18-24";
//定义规则
String regex = "-";
//调用方法
String[] strArray = ages.split(regex);
//遍历
// for(int x=0;x <strArray.length;x++){
// System.out.println(strArray[x]);
// }
//如何得到int类型的呢?
int startAge = Integer.parseInt(strArray[0]);
int endAge = Integer.parseInt(strArray[1]);
//键盘录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
if(age >=startAge&&age<=endAge){
System.out.println("你就是我想要的");
}
else{
System.out.println("你不是我要找的");
}
}
}
运行结果:
练习3:
public class RegexDemo2 {
public static void main(String[] args) {
//定义一个字符串
String s1 = "aa,bb,cc";
String[] str1Array = s1.split(",");
for(int x = 0 ;x <str1Array.length;x++){
System.out.println(str1Array[x]);
}
System.out.println("---------------");
String s2 = "aa.bb.cc";
String[] str2Array = s2.split("\\.");
for(int x = 0 ;x <str1Array.length;x++){
System.out.println(str1Array[x]);
}
System.out.println("---------------");
String s3 = "aa bb cc";
String[] str3Array = s3.split(" +");
for(int x = 0 ;x <str1Array.length;x++){
System.out.println(str1Array[x]);
}
System.out.println("---------------");
//硬盘上的路径,我们应该用\\替代\
String s4 = "F:\\java第二季\\day14\\code\\day14_Regex\\src\\cn\\itcast_03";
String[] str4Array = s4.split("\\\\");
for(int x = 0 ;x <str1Array.length;x++){
System.out.println(str1Array[x]);
}
System.out.println("---------------");
}
}
运行结果:
练习4:
/*
* 我有如下一个字符串:"91 27 46 38 50"
* 请写代码实现最终输出结果是:"27 38 46 50 91"
*
* 分析:
* A:定义一个字符串
* B:把字符串进行分割,得到一个字符串数组
* C:把字符串数组变换成int数组
* D:对int数组排序
* E:把排序后的int数组在组装成一个字符串
* F:输出字符串
*/
public class RegexTest {
public static void main(String[] args) {
// 定义一个字符串串
String s = "91 27 46 38 50";
// 把字符串进行分割
String[] strArray = s.split(" ");
// 把字符串数组变为int数组
int[] arr = new int[strArray.length];
for (int x = 0; x < arr.length; x++) {
arr[x] = Integer.parseInt(strArray[x]);
}
// 对int数组进行排序
bubbleSort(arr);
//seceltSort(arr);
// 把排序后的int数组在组装成一个字符串
/*String result = "";
for (int x = 0; x < arr.length; x++) {
String str = String.valueOf(arr[x]);
result += str;
result +=" ";
}*/
StringBuilder sb = new StringBuilder();
for(int x = 0;x <arr.length;x++){
sb.append(arr[x]).append(" ");
}
String result = sb.toString().trim();
System.out.println(result);
}
// 冒泡排序
public static void bubbleSort(int[] arr) {
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
}
}
运行结果:
4.8.4 正则表达式的替换功能
String类的public String replaceAll(String regex,String replacement)
/*
* 替换功能
* String类的public String replaceAll(String regex,String replacement)
* 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
*/
public class RegexDemo {
public static void main(String[] args) {
//定义一个字符串
String s = "hello1234worldkh6212260502006823456";
//我要去除所有的数字,用“*”替换
String regex = "\\d";
String ss = "*";
String result = s.replaceAll(regex, ss);
System.out.println(result);
}
}
运行结果:
4.8.5 正则表达式的获取功能
Pattern类和Matcher类的使用
/*
* 获取功能:
* 获取下面这个字符串中由三个字符组成的单词
* da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
*/
public class RegexDemo2 {
public static void main(String[] args) {
// 定义字符串
String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
// 规则
String regex = "\\b\\w{3}\\b";
// 把规则编译成模式对象
Pattern p = Pattern.compile(regex);
// 通过模式对象得到匹配器对象
Matcher m = p.matcher(s);
// 调用匹配器的功能
// 通过find方法查找有没有满足的子序列
// public boolean find()
/*
* boolean flag = m.find(); System.out.println(flag); //如何得到值呢? //public
* String group() String ss = m.group(); System.out.println(ss);
*
* flag = m.find(); ss = m.group(); System.out.println(ss);
*/
while (m.find()) {
System.out.println(m.group());
}
// 注意:一定要先find(),然后才能group()
// IllegalStateException: No match found
// String ss = m.group();
// System.out.println(ss);
}
}
运行结果: