一、数组 [ ]
数组:可以存放多个同一类型的数据
数组的下标从 0 开始
1. 动态初始化 - 确定元素数量
方式 1
1 2 3 4
| 数组类型 数组名[] = new 数组类型[大小]
int a[] = new int[5];
|
方式 2
1 2 3 4 5
| 数组类型 数组名[]; 或 数组类型[] 数组名; 数组名 = new 数据类型[大小];
int a[]; a = new int[5];
|
2.静态初始化 - 不确定元素数量
1 2 3
| 数据类型 数组名[] = {元素,元素,元素...}
int a[] = {2,5,7,8,99,2,4,5,};
|
- 数组是多个相同类型的数据组合,不同类型的数据不能放到同一数组中
- 数组创建后,如果没有赋值,则默认值为 byte、short、int、long 为 0,float、double 为 0.0,char 为\u0000, boolean 为 false, String 为 null
- 数组的下标从 0 开始
- 不同类型存放时需要遵循 数据自动类型转换 规则
3. 数组拷贝
1 2 3 4 5
| int a = 10; int b = a;
b = 80;
|
1 2 3 4 5
| int a[] = {1,2,3}; int b[] = a;
b[0] = 10;
|
值拷贝不会改变原来的数据,引用赋值会改变原始数据,赋值实际上是原始数据的内存地址;
如果需要得到不影响原始数据的数组,可以通过遍历获取新的数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class demo01{ public static void main(String[] args) { int arr1[] = {1,2,3}; int arr2[] = new int[arr1.length];
for(int i = 0; i< arr1.length; i++){ arr2[i] = arr1[i]; }
arr2[0] = 999; for (int i = 0; i<arr2.length; i++) { System.out.println(arr2[i]); System.out.println(arr1[i]); } } }
|
4.数组反转
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class demo01{ public static void main(String[] args) { int arr1[] = {11,22,33,44,55,66}; int num = arr1.length; for (int i = 0; i< num/2; i++ ) { int temp = arr1[i]; arr1[i] = arr1[num-1-i]; arr1[num-1-i] = temp; } for (int i = 0; i<=arr1.length-1; i++ ) { System.out.println(arr1[i]); } } }
|
5.数组添加
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class demo01{ public static void main(String[] args) { int arr1[] = {1,2,3}; int arr2[] = new int[arr1.length + 1]; for (int i = 0; i< arr1.length;i++ ) { arr2[i] = arr1[i]; } arr2[arr2.length -1] = 4; for (int i = 0; i< arr2.length;i++ ) { System.out.print(arr2[i] + " "); } } }
|
二、排序
排序: 将多个数据,依指定的顺序进行排序的过程
- 内部排序
- 将需要处理的数据加载到内存中进行排序
- 交换式排序、选择排序、插入式排序
- 外部排序
- 数据量过大,需要借助外部存储进行排序
- 合并排序,直接合并排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class demo01{ public static void main(String[] args) { int arr[] = {24,69,80,587,13}; for (int i = 0; i <arr.length-1 ;i++ ) { for (int j = 0; j< arr.length -i -1;j++ ) { if (arr[j] > arr[j+1]) { int tamp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tamp; } } } for (int i = 0; i < arr.length ;i++ ) { System.out.print(arr[i]+ "\t"); } } }
|
三、查找
1.按顺序查找
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.util.Scanner; public class demo01{ public static void main(String[] args) { String arr[] = {"白眉鹰王","金毛狮王","紫衫龙王","青翼蝠王"}; Scanner myScanner = new Scanner(System.in);
System.out.print("请输入明教护法的名字:"); String name = myScanner.next();
for (int i = 0; i < arr.length -1; i++) { if (name.equals(arr[i])) { System.out.print("找到了,下标为" + i); return; } } } }
|
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 30
|
public static int binarySearch2(int[]arr,int dest) {
int begin = 0; int end = arr.length-1;
while (begin <= end){ int mid = (begin + end) / 2; if (arr[mid] == dest){ return mid; }else if (arr[mid] < dest){ begin = mid + 1; }else { end = mid - 1; } } return -1; }
|
四、二维数组
1 2 3 4
| 类型[][] 数组名 = new 类型[大小][大小] 数组类型[][] 数组名 = {{元素,元素},{元素,元素},{元素,元素}};
int[][] arr = {{0,1,2},{1,2,3},{7,6,3}};
|
1 2 3 4 5 6 7 8 9 10 11
| public class demo01{ public static void main(String[] args) { int[][] arr = {{0,0,0,0,0,0},{0,0,1,0,0,0},{0,2,0,3,0,0},{0,0,0,0,0,0}}; for (int i=0; i<arr.length; i++) { for (int j = 0; j<arr[i].length; j++) { System.out.print(arr[i][j]); } System.out.println(); } } }
|
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
| public class demo01{ public static void main(String[] args) { int arr[][] = new int[10][]; for (int i=0; i<arr.length; i++) {
arr[i] = new int[i+1];
for (int j=0;j<arr[i].length ;j++ ) { if (j == 0 || j == arr[i].length -1) { arr[i][j]=1; }else { arr[i][j] = arr[i-1][j] + arr[i-1][j-1]; } } } for (int i=0; i<arr.length; i++) { for (int j=0; j<arr[i].length; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } }
|