数组(Array)本身是引用数据类型
特点:
数组一旦初始化完成,其长度就确定了。
//1.1 静态初始化:数组的初始化和数组元素的赋值操作同时进行
int[] ids = new int[]{1001,1002,1003,1004}; //new为之分配空间
//int[] ids = {1001,1002,1003,1004}; //类型推断(PS:此时必须是声明+初始化
//1.2动态初始化:数组的初始化和数组元素的赋值操作分开进行
String[] names = new String[5];
上面是标准写法(声明+初始化),也可分开:
String[] names; //声明
names = new String[5]; //初始化
数组元素的默认初始化值:
//静态初始化
int[][] arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}};
//int[] arr4[] = new int[][]{{1,2,3},{4,5,9,10},{6,7,8}};
//int[] arr5[] = {{1,2,3},{4,5},{6,7,8}};
//动态初始化1
String[][] arr2 = new String[3][2];
//动态初始化2
String[][] arr3 = new String[3][];
二维数组元素的默认初始化值:
针对于初始化方式一:比如:int[][] arr = new int[4][3];
针对于初始化方式二:比如:int[][] arr = new int[4][];
String[][] arr3 = new String[3][];
arr3[1] = new String[4]; //初始化内层数组,该内层地址才存储外层arr3中,否则内层无法访问
System.out.println(arr3[1]); //内层地址值
System.out.println(arr3[1][0]);//null 因为元素是引用类型String,默认初始化值为null
常用几个方法如下,其余件API文档。PS:Arrays.equals(arr1,arr2);判断的是这两个数组元素是否对应相等,而不是地址值“==”;赋值时 “=”,赋的是地址,即原对象又多了一个引用。
声明:int[] x,y[]; 在给x,y变量赋值以后,以下选项允许通过编译的是:(赋值需满足同类型或类型自动提升
//数组元素反转
for(int i = 0,j = arr.length - 1;i < j;i++,j--){
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
//杨辉三角
//1.声明并初始化二维数组
int[][] arrYangHui = new int[10][];
//2.给数组的元素赋值
for (int i = 0; i < arrYangHui.length; i++) {
arrYangHui[i] = new int[i+1];
//2.1 给首末元素赋值
arrYangHui[i][0] = 1;
arrYangHui[i][i] = 1;
//2.2 给每行的非首末元素赋值
if (i >= 2) {
for (int j = 1; j <= i-1; j++) {
arrYangHui[i][j] = arrYangHui[i - 1][j - 1] + arrYangHui[i - 1][j];
}
}
}
01 02 03 04 05 06 07
24 25 26 27 28 29 08
23 40 41 42 43 30 09
22 39 48 49 44 31 10
21 38 47 46 45 32 11
20 37 36 35 34 33 12
19 18 17 16 15 14 13
//回形数
int n = 7;
int[][] arr = new int[n][n];
int count = 0; // 要显示的数据
int maxX = n - 1; // x轴的最大下标
int maxY = n - 1; // Y轴的最大下标
int minX = 0; // x轴的最小下标
int minY = 0; // Y轴的最小下标
while (minX <= maxX) {
for (int x = minX; x <= maxX; x++) {
arr[minY][x] = ++count;
}
minY++;
for (int y = minY; y <= maxY; y++) {
arr[y][maxX] = ++count;
}
maxX--;
for (int x = maxX; x >= minX; x--) {
arr[maxY][x] = ++count;
}
maxY--;
for (int y = maxY; y >= minY; y--) {
arr[y][minX] = ++count;
}
minX++;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
String space = (arr[i][j] + "").length() == 1 ? "0" : "";
System.out.print(space + arr[i][j] + " ");
}
System.out.println();
}
//冒泡排序:两两比较,大数下沉 //具体排序等算法中再涉及(快排目前性能较优
int[] arr = new int[]{43,32,76,-98,0,64,33,-21,32,99};
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 temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
//二分法查找:(熟悉)
//前提:所要查找的数组必须有序。
int[] arr2 = new int[]{-98,-34,2,34,54,66,79,105,210,333};
int dest1 = -34;
int head = 0;//初始的首索引
int end = arr2.length - 1;//初始的末索引
boolean isFlag1 = true;
while(head <= end){
int middle = (head + end)/2;
if(dest1 == arr2[middle]){
System.out.println("找到了指定的元素,位置为:" + middle);
isFlag1 = false;
break;
}else if(arr2[middle] > dest1){
end = middle - 1;
}else{//arr2[middle] < dest1
head = middle + 1;
}
}
if(isFlag1){
System.out.println("很遗憾,没有找到的啦!");
}