1:数组高级以及Arrays(掌握)
(1)排序
A:冒泡排序
相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。同理,其他的元素就可以排好。
/*
* 数组排序之冒泡排序:
* 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处
*/
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);
/*
// 第一次比较
// arr.length - 1是为了防止数据越界
// arr.length - 1 - 0是为了减少比较的次数
for (int x = 0; x < arr.length - 1 - 0; x++) {
if (arr[x] > arr[x + 1]) {
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
}
}
System.out.println("第一次比较后:");
printArray(arr);
// 第二次比较
// arr.length - 1是为了防止数据越界
// arr.length - 1 - 1是为了减少比较的次数
for (int x = 0; x < arr.length - 1 - 1; x++) {
if (arr[x] > arr[x + 1]) {
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
}
}
System.out.println("第二次比较后:");
printArray(arr);
// 第三次比较
// arr.length - 1是为了防止数据越界
// arr.length - 1 - 2是为了减少比较的次数
for (int x = 0; x < arr.length - 1 - 2; x++) {
if (arr[x] > arr[x + 1]) {
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
}
}
System.out.println("第三次比较后:");
printArray(arr);
// 第四次比较
// arr.length - 1是为了防止数据越界
// arr.length - 1 - 3是为了减少比较的次数
for (int x = 0; x < arr.length - 1 - 3; x++) {
if (arr[x] > arr[x + 1]) {
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
}
}
System.out.println("第四次比较后:");
printArray(arr);
*/
// 既然听懂了,那么上面的代码就是排序代码
// 而上面的代码重复度太高了,所以用循环改进
// for (int y = 0; y < 4; y++) {
// for (int x = 0; x < arr.length - 1 - y; x++) {
// if (arr[x] > arr[x + 1]) {
// int temp = arr[x];
// arr[x] = arr[x + 1];
// arr[x + 1] = temp;
// }
// }
// }
/*
// 由于我们知道比较的次数是数组长度-1次,所以改进最终版程序
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;
}
}
}
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("]");
}
}
B:选择排序
把0索引的元素,和索引1以后的元素都进行比较,第一次完毕,最小值出现在了0索引。同理,其他的元素就可以排好。
/*
* 数组排序之选择排序:
* 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
*/
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);
/*
// 第一次
int x = 0;
for (int y = x + 1; y < arr.length; y++) {
if (arr[y] < arr[x]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
System.out.println("第一次比较后:");
printArray(arr);
// 第二次
x = 1;
for (int y = x + 1; y < arr.length; y++) {
if (arr[y] < arr[x]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
System.out.println("第二次比较后:");
printArray(arr);
// 第三次
x = 2;
for (int y = x + 1; y < arr.length; y++) {
if (arr[y] < arr[x]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
System.out.println("第三次比较后:");
printArray(arr);
// 第四次
x = 3;
for (int y = x + 1; y < arr.length; y++) {
if (arr[y] < arr[x]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
System.out.println("第四次比较后:");
printArray(arr);
*/
/*
//通过观察发现代码的重复度太高,所以用循环改进
for(int x=0; x<arr.length-1; x++){
for(int y=x+1; y<arr.length; y++){
if(arr[y] <arr[x]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
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+1; y<arr.length; y++){
if(arr[y] <arr[x]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = 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)查找
A:基本查找
针对数组无序的情况
public static int getIndex(int[] arr,int value) {
int index = -1;
for(int x=0; x<arr.length; x++) {
if(arr[x] == value) {
index = x;
break;
}
}
return index;
}
B:二分查找(折半查找)
针对数组有序的情况(千万不要先排序,在查找)
/*
* 查找:
* 基本查找:数组元素无序(从头找到尾)
* 二分查找(折半查找):数组元素有序
*
* 分析:
* A:定义最大索引,最小索引
* B:计算出中间索引
* C:拿中间索引的值和要查找的值进行比较
* 相等:就返回当前的中间索引
* 不相等:
* 大 左边找
* 小 右边找
* D:重新计算出中间索引
* 大 左边找
* max = mid - 1;
* 小 右边找
* min = mid + 1;
* E:回到B
*/
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;
}
}
(3)Arrays工具类
A:是针对数组进行操作的工具类。包括排序和查找等功能。
B:要掌握的方法(自己补齐方法)
把数组转成字符串:
排序:
二分查找:
/*
* 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));
// [13, 24, 57, 69, 80]
// public static int binarySearch(int[] a,int key) 二分查找
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57));
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
}
}
(4)Arrays工具类的源码解析
(5)把字符串中的字符进行排序
举例:
"edacbgf"
得到结果
"abcdefg"
/*
* 把字符串中的字符进行排序。
* 举例:"dacgebf"
* 结果:"abcdefg"
*
* 分析:
* A:定义一个字符串
* B:把字符串转换为字符数组
* C:把字符数组进行排序
* D:把排序后的字符数组转成字符串
* E:输出最后的字符串
*/
public class ArrayTest {
public static void main(String[] args) {
// 定义一个字符串
String s = "dacgebf";
// 把字符串转换为字符数组
char[] chs = s.toCharArray();
// 把字符数组进行排序
bubbleSort(chs);
//把排序后的字符数组转成字符串
String result = String.valueOf(chs);
//输出最后的字符串
System.out.println("result:"+result);
}
// 冒泡排序
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;
}
}
}
}
}
4:Character(了解)
(1)Character构造方法
Character ch = new Character(‘a’);
/*
* Character 类在对象中包装一个基本类型 char 的值
* 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然
*
* 构造方法:
* Character(char value)
*/
public class CharacterDemo {
public static void main(String[] args) {
// 创建对象
// Character ch = new Character((char) 97);
Character ch = new Character('a');
System.out.println("ch:" + ch);
}
}
(2)要掌握的方法:(自己补齐)
A:判断给定的字符是否是大写
B:判断给定的字符是否是小写
C:判断给定的字符是否是数字字符
D:把给定的字符转成大写
E:把给定的字符转成小写
/*
* 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):把给定的字符转换为小写字符
*/
public class CharacterDemo {
public static void main(String[] args) {
// public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符
System.out.println("isUpperCase:" + Character.isUpperCase('A'));
System.out.println("isUpperCase:" + Character.isUpperCase('a'));
System.out.println("isUpperCase:" + Character.isUpperCase('0'));
System.out.println("-----------------------------------------");
// public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
System.out.println("isLowerCase:" + Character.isLowerCase('A'));
System.out.println("isLowerCase:" + Character.isLowerCase('a'));
System.out.println("isLowerCase:" + Character.isLowerCase('0'));
System.out.println("-----------------------------------------");
// public static boolean isDigit(char ch):判断给定的字符是否是数字字符
System.out.println("isDigit:" + Character.isDigit('A'));
System.out.println("isDigit:" + Character.isDigit('a'));
System.out.println("isDigit:" + Character.isDigit('0'));
System.out.println("-----------------------------------------");
// public static char toUpperCase(char ch):把给定的字符转换为大写字符
System.out.println("toUpperCase:" + Character.toUpperCase('A'));
System.out.println("toUpperCase:" + Character.toUpperCase('a'));
System.out.println("-----------------------------------------");
// public static char toLowerCase(char ch):把给定的字符转换为小写字符
System.out.println("toLowerCase:" + Character.toLowerCase('A'));
System.out.println("toLowerCase:" + Character.toLowerCase('a'));
}
}
(3)案例:
统计字符串中大写,小写及数字字符出现的次数
/*
* 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
*
* 分析:
* A:定义三个统计变量。
* int bigCont=0;
* int smalCount=0;
* int numberCount=0;
* B:键盘录入一个字符串。
* C:把字符串转换为字符数组。
* D:遍历字符数组获取到每一个字符
* E:判断该字符是
* 大写 bigCount++;
* 小写 smalCount++;
* 数字 numberCount++;
* F:输出结果即可
*/
public class CharacterTest {
public static void main(String[] args) {
// 定义三个统计变量。
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
// 键盘录入一个字符串。
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
// 把字符串转换为字符数组。
char[] chs = line.toCharArray();
// 历字符数组获取到每一个字符
for (int x = 0; x < chs.length; x++) {
char ch = chs[x];
// 判断该字符
if (Character.isUpperCase(ch)) {
bigCount++;
} else if (Character.isLowerCase(ch)) {
smallCount++;
} else if (Character.isDigit(ch)) {
numberCount++;
}
}
// 输出结果即可
System.out.println("大写字母:" + bigCount + "个");
System.out.println("小写字母:" + smallCount + "个");
System.out.println("数字字符:" + numberCount + "个");
}
}