数组
注意:一旦初始化完成,数组的长度就确定了且!不可更改
创建时,在内存中开辟一整块"连续的空间",
二维数组
java
int[][] arr1 = new int[][]{{123},{321,12}};
int[][] arr3 = new int[2][]; // 因为内层未声明,外层元素,默认存储null
// int 外层 内层 变量名 = new int 外层 内层;
/**
声明两个一维数组x,y;
*/
int[] x,y;
// =====
int[] x,y[];
//y是二维,
// x是一维;
常见算法
1、数值型数据的特征统计;(均值,最大、小值;总和)
java
public static void main(String[] args) {
// 一、求特征值;
/*一个数组,10个元素,随机整数,求出最大、小、总和;*/
/*1、动态初始化*/
int[] arr = new int[10];
/*2、循环赋值*/
for (int i = 0; i < arr.length; i++) {
// arr[i] = (int)(Math.random() * (99 - 10 + 1) + 10); [10-99]
arr[i] = (int)(Math.random() * 90 + 10);
// System.out.println(arr[i]);
}
/*3、求最大值*/
int max = arr[0];
for (int j = 1; j < arr.length; j++) {
if(arr[j] > max) {
max = arr[j];
}
}
System.out.println("最大值为:" + max);
/*4、求最小值*/
// 略;
/*5、求总和*/
int total = 0;
for (int i = 0; i < arr.length; i++) {
total += arr[i];
}
System.out.println("总和为" + total);
/*6、平均值*/
int avg = total / arr.length;
System.out.println(avg);
}
public static void main(String[] args) {
// 特征值;
/*评委打分:掐头去尾,求平均值;*/
int[] scoreArr = new int[] {5,4,8,9,0,1,2,7,3};
int max = scoreArr[0], total = 0, min = scoreArr[0];
int avg = 0;
for (int i = 0; i < scoreArr.length; i++) {
total += scoreArr[i];
if(max < scoreArr[i]) {
max = scoreArr[i];
}
if(min > scoreArr[i]) {
min = scoreArr[i];
}
}
avg = (total - max -min ) / (scoreArr.length - 2);
System.out.println(avg);
}
2、数组元素的赋值;
java
public static void main(String[] args) {
// 数组元素的赋值;
/*打印一个10行的, 杨辉三角*/
/*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 */
int[][] arr = new int[10][];
for (int i = 0; i < arr.length; i++) {
/*定义长度*/
arr[i] = new int[i+1];
/*首尾赋值为1*/
arr[i][0] = 1;
arr[i][arr[i].length - 1] = 1; //看图形相当于arr[i][i];
// 条件不满足的不会进来;
for (int j = 1; j < arr[i].length - 1; j++) {
arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
}
}
/*for (int i = 2; i < arr.length; i++) {
for (int j = 1; j < arr[i].length - 1; j++) {
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] + "\t");
}
System.out.println();
}
// System.out.println();
}
3、数组的复制和反转;
引用类型的问题,同JS相同;修改同一个地址后,多个引用同一地址会变动;
一旦初始化完成,数组的长度就确定了且!不可更改
java
public static void main(String[] args) {
// 数组元素的复制;
int[] arr1 = new int[]{1,2,3,4,5,6};
int[] arr2 = new int[arr1.length];
for (int i = 0; i < arr2.length; i++) {
arr2[i] = arr1[i];
}
System.out.println("==================");
/*数组反转*/
int[] posArr = new int[] {1,2,3,4,5,6};
int[] nagArr = new int[posArr.length];
for (int i = 0; i < posArr.length; i++) {
nagArr[posArr.length - 1 - i] = posArr[i];
}
for (int i = 0; i < nagArr.length; i++) {
System.out.print(nagArr[i]);
}
System.out.println("=================");
/*数组反转*/
// arr[i] = arr[arr.length - 1 - i]
for (int i = 0; i < posArr.length / 2; i++) {
int temp = posArr[i];
posArr[i] = posArr[posArr.length - 1 - i];
posArr[posArr.length - 1 - i] = temp;
}
for (int i = 0; i < nagArr.length; i++) {
System.out.print(posArr[i]);
}
System.out.println("=================");
/*数组反转*/
for (int i = 0,j=posArr.length - 1; i < j ; i++, j--) {
int mid = posArr[i];
posArr[i] = posArr[j];
posArr[j] = mid;
}
for (int i = 0; i < nagArr.length; i++) {
System.out.print(posArr[i]);
}
}
4、扩容和缩容;(扩展长度)
java
public static void main(String[] args) {
/*扩容并添加值10,20,30*/
int[] arr = new int[] {1,2,3,4,5};
// int[] newArr = new int[arr.length * 2];
int[] newArr = new int[arr.length << 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
newArr[arr.length] = 10;
newArr[arr.length+1] = 20;
newArr[arr.length+2] = 30;
arr = newArr;
/*GC判断newArr无指针后会进行回收,*/
System.out.println("===================");
/*缩容,删除第n项*/
int index = 4;
// 方式1、
int[] oArr = new int[] {1,2,3,4,5,6,7};
for (int i = index; i < oArr.length - 1; i++) {
oArr[i] = oArr[i+1];
}
oArr[oArr.length-1] = 0;
for (int i = 0; i < oArr.length; i++) {
System.out.print(oArr[i] + "\t");
}
System.out.println("===================");
// 方式2、
int[] ooArr = new int[] {1,2,3,4,5,6,7};
int[] nArr = new int[ooArr.length - 1];
for (int i = 0; i < index; i++) {
nArr[i] = ooArr[i];
}
for (int i = index; i < ooArr.length - 1; i++) {
nArr[i]= ooArr[i + 1];
}
ooArr = nArr;
for (int i = 0; i < ooArr.length; i++) {
System.out.print(ooArr[i] + "\t");
}
}
5.元素的查找(线性、二分)
java
public static void main(String[] args) {
/*线性查找*/
int[] arr = new int[] {1,2,3,4,5,6,7,8,9,0};
int target = 5,index = -1;
for (int i = 0; i < arr.length; i++) {
if(arr[i] == target) {
index = i;
break;
}
}
System.out.println("index: " + index);
System.out.println("=================");
/*二分查找*/
// 前提是一个有序数组;
int[] arr2 = new int[] {1,2,3,4,5,6,7,8,34,44,45,56,67,78,89,90};
// int target2 = 8; //查找目标;
int target2 = 99; //查找目标;
int head = 0, end = arr2.length - 1; //首尾索引;
int index2 = -1;
while (head <= end) {
/*计算中间值*/
int mid = (head + end) / 2;
if(target2 == arr2[mid]) {
index2 = mid;
break;
} else if(target2 > arr2[mid]) {
/*查找右边*/
head = mid + 1;
} else {
/*查找左边*/
end = mid - 1;
}
}
System.out.println("index2: " + index2);
}
6.数组排序
java
public static void main(String[] args) {
/*排序*/
/**1、冒泡排序:
* 相邻的比较,大的就往后移动;
* N轮;
* (每次冒个最大的泡泡)
*/
int[] arr = new int[] {1,2,33,4,5,6,7,8,9,0};
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 mid = arr[j];
arr[j] = arr[j+1];
arr[j+1] = mid;
}
}
}
System.out.print("冒泡排序:");
for (int i = 0; i < arr.length ; i++) {
System.out.print(arr[i] +"\t");
}
System.out.println();
System.out.println("===================");
/*快速排序*/
/**
*
*/
System.out.print("快速排序:");
}
关于部分术语
java
1、时间复杂度:分析关键字的比较次数和记录的移动次数
:O(1)<log2n < n N nlog2n < n^2 < n^3 <2^n < n! < n^n;
2、空间复杂度:分析排序算法中需要多少辅助内存;
很多时候都是用 ""空间换时间。相对来说时间比较重要;
3、稳定性:排序后相等的值前后顺序是否可能会变化;