# 1 - 数组
# 1.1 - 数组的概念
数组本质上就是在内存空间中申请一块连续的存储空间,用来记录多个类型相同的数据。
# 1.2 - 数组的创建
定义方式
1 2 格式: 数据类型[] 数组名称 = new 数据类型[数组的长度];int [] arr = new int [10 ];
初始化方式
1 2 格式: 数据类型[] 数组名称 = {成员1 ,成员2 ,成员3 ,成员4. ....};int [] arr = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,0 };
两种的结合方式
1 int [] arr = new int []{0 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1 };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import java.util.Arrays;public class Demo04 { public static void main (String[] args) { int [] arr01 = new int [10 ]; System.out.println(arr01); System.out.println(Arrays.toString(arr01)); int [] arr02 = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,0 }; System.out.println(Arrays.toString(arr02)); int [] arr03 = new int []{0 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1 }; System.out.println(Arrays.toString(arr03)); } }
# 1.3 - 数组的访问与赋值
访问与赋值时通过下标 来确定位置的,下标范围 0 ~ 数组的长度减 1 结束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import java.util.Arrays;public class Demo05 { public static void main (String[] args) { int [] arr01 = new int [10 ]; System.out.println("数组中的默认值:" + Arrays.toString(arr01)); arr01[5 ] = 6 ; arr01[8 ] = 9 ; System.out.println("数组赋值后的值:" + Arrays.toString(arr01)); int [] arr02 = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,0 }; System.out.println(arr02[3 ]); System.out.println(arr02[7 ]); } }
# 1.4 - 数组中的默认值
当数组定义完成后,数组会根据数据类型的不容而创建不同的默认值
数组中的数据类型
默认值
byte short int long
0
float double
0.0
boolean
false
char
‘\u0000’ 空
String
null
# 1.5 - 数组的特性
1、数组中的成员占用的是连续的存储空间
2、数组名实为该数组的首地址
3、数组的成员访问时,注意不要下标越界
4、数组中的成员数据类型必须相同
5、Java中的数组的长度一旦确定不能增减
# 1.6 - 数组的遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Demo06 { public static void main (String[] args) { int [] score = {60 ,66 ,70 ,80 ,88 ,90 ,99 ,100 }; for (int i = 0 ; i < score.length; i++) { System.out.print("score[" + i + "] = " + score[i] + "\t" ); } System.out.println(); System.out.println("----------------------------------------------------------" ); for (int i = score.length - 1 ; i >= 0 ; i--) { System.out.print("score[" + i + "] = " + score[i] + "\t" ); } } }
# 1.7 - 数组的扩容以及拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import java.util.Arrays;public class Demo07 { public static void main (String[] args) { int [] arr01 = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,0 }; int [] arr02 = new int [arr01.length * 2 ]; for (int i = 0 ; i < arr01.length; i++) { arr02[i] = arr01[i]; } System.out.println("arr01:" + Arrays.toString(arr01)); System.out.println("arr02:" + Arrays.toString(arr02)); System.out.println("----------------------------------------------------------------" ); int [] arr03 = Arrays.copyOf(arr01, arr01.length * 2 ); System.out.println("arr01:" + Arrays.toString(arr01)); System.out.println("arr03:" + Arrays.toString(arr03)); } }
# 1.8 - 数组中的经典算法
# 1.8.1 - 求最值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import java.util.Arrays;import java.util.Random;public class Demo01 { public static void main (String[] args) { Random ran = new Random (); int [] arr = new int [10 ]; for (int i = 0 ; i < arr.length; i++) { arr[i] = ran.nextInt(100 ); } int max = arr[0 ]; for (int i = 1 ; i < arr.length; i++) { max = arr[i] > max ? arr[i] : max; } System.out.println(Arrays.toString(arr)); System.out.println("Max:" + max); } }
# 1.8.2 - 查找数组中指定的成员的值(所在位置)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import java.util.Arrays;import java.util.Scanner;public class Demo02 { public static void main (String[] args) { int [] arr = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,6 ,0 }; System.out.println("数组中的成员有:" + Arrays.toString(arr)); System.out.println("请输入要查找的值:" ); int num = new Scanner (System.in).nextInt(); int resIndex = 0 ; for (int i = 0 ; i < arr.length; i++) { if (num == arr[i]){ resIndex = i; System.out.println(arr[resIndex] + "在数组中出现的位置是:[" + resIndex + "]" ); } } } }
# 1.8.3 - 数组的逆序存储
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class Demo03 { public static void main (String[] args) { int [] arr = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 }; System.out.print("逆序存储之前:" ); for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + "\t" ); } for (int i = 0 ,t; i < arr.length / 2 ; i++) { t = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = t; } System.out.println(); System.out.print("逆序存储之后:" ); for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + "\t" ); } } }
# 1.8.4 - 数组的排序(冒泡排序法升序)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.util.Arrays;import java.util.Random;public class Demo04 { public static void main (String[] args) { Random ran = new Random (); int [] arr = new int [10 ]; for (int i = 0 ; i < arr.length; i++) { arr[i] = ran.nextInt(100 ); } System.out.println("数组中的成员有:" + Arrays.toString(arr)); for (int i = 0 ; i < arr.length - 1 ; i++) { for (int j = 0 ; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1 ]){ int t = arr[j]; arr[j] = arr[j + 1 ]; arr[j + 1 ] = t; } } } System.out.println("冒 泡 排序 后:" + Arrays.toString(arr)); } }
# 1.8.5 - 回文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.util.Scanner;public class Demo05 { public static void main (String[] args) { System.out.println("请输入一串字符串:" ); String str = new Scanner (System.in).nextLine(); int i; for (i = 0 ; i < str.length() / 2 ; i++){ if (str.charAt(i) != str.charAt(str.length() - 1 - i)){ break ; } } if (i < (str.length() / 2 )){ System.out.println(str + "不是回文字符串" ); }else { System.out.println(str + "是回文字符串" ); } } }
# 1.8.6 - 向有序数组中插入成员后仍保证数组有序
1 int [] arr = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,0 };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 import java.util.Arrays;import java.util.Scanner;public class Demo01 { public static void main (String[] args) { int [] arr = new int [10 ]; for (int i = 0 ; i < arr.length - 1 ; i++) { arr[i] = i + 1 ; } System.out.println("插入前:" + Arrays.toString(arr)); System.out.println("请输入要插入的数值:" ); int num = new Scanner (System.in).nextInt(); int place; for (place = 0 ;place < arr.length - 1 ;place++){ if (num < arr[place]){ break ; } } for (int i = arr.length - 2 ; i >= place ; i--) { arr[i + 1 ] = arr[i]; } arr[place] = num; System.out.println("插入后:" + Arrays.toString(arr)); } }
# 2 - 二维数组
# 2.1 - 二维数组的概念
二维数组本质上就是由一维数组组成的数组,也就是说数组中的成员都是一个数组。
# 2.2 - 数组创建
定义方式
1 2 3 int [][] arr = new int [2 ][3 ]; * * * * * *
初始化方式
1 2 3 int [][] arr = {{1 ,2 ,3 },{4 ,5 ,6 }};1 2 3 4 5 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import java.util.Arrays;public class Demo06 { public static void main (String[] args) { int [][] arr = new int [2 ][3 ]; for (int i = 0 ; i < 2 ; i++) { for (int j = 0 ; j < 3 ; j++) { System.out.print(arr[i][j] + "\t" ); } System.out.println(); } System.out.println("-------------------------------" ); int [][] arr02 = {{1 ,2 ,3 },{4 ,5 ,6 }}; for (int i = 0 ; i < 2 ; i++) { for (int j = 0 ; j < 3 ; j++) { System.out.print(arr02[i][j] + "\t" ); } System.out.println(); } } }
# 2.3 - 二维数组的赋值和访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public class Demo07 { public static void main (String[] args) { int [][] arr01 = new int [5 ][5 ]; for (int i = 0 ; i < 5 ; i++) { for (int j = 0 ; j < 5 ; j++) { arr01[i][j] = j + 1 ; } } for (int i = 0 ; i < 5 ; i++) { for (int j = 0 ; j < 5 ; j++) { System.out.print(arr01[i][j] + "\t" ); } System.out.println(); } arr01[2 ][3 ] = 0 ; arr01[3 ][2 ] = 0 ; System.out.println("---------------------------" ); for (int i = 0 ; i < 5 ; i++) { for (int j = 0 ; j < 5 ; j++) { System.out.print(arr01[i][j] + "\t" ); } System.out.println(); } System.out.println(arr01[2 ][4 ]); } }
# 2.4 - 二维数组 - 矩阵转置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public class Demo08 { public static void main (String[] args) { int [][] arr = {{1 ,2 ,3 },{4 ,5 ,6 },{7 ,8 ,9 }}; for (int i = 0 ; i < 3 ; i++) { for (int j = 0 ; j < 3 ; j++) { System.out.print(arr[i][j] + "\t" ); } System.out.println(); } for (int i = 0 ; i < 3 ; i++) { for (int j = 0 ; j < 3 ; j++) { if (j > i){ arr[i][j] ^= arr[j][i]; arr[j][i] ^= arr[i][j]; arr[i][j] ^= arr[j][i]; } } } System.out.println("-------------------" ); for (int i = 0 ; i < 3 ; i++) { for (int j = 0 ; j < 3 ; j++) { System.out.print(arr[i][j] + "\t" ); } System.out.println(); } } }