知识点1:数组的理解
* 1. 数组,可以看做是多个相同数据类型类型的变量按照一定顺序依次紧密排列起来的集合。
*
* 2.数组的相关概念:
* 数组名
* 数组元素
* 数组的长度
* 数组的角标、下标、下角标、索引、index
*
* 3. 回顾:变量按照数据类型来分:基本数据类型、引用数据类型(数组、类、接口)
*
* >数组,属于引用数据类型;数组的元素,既可以是基本数据类型,也可以是引用数据类型。
* >数组,一旦初始化完成,其长度就是确定的。且长度不可变
知识点2:一维数组的使用
一维数组的声明和初始化
4.1 一维数组的声明和初始化
int num
;
num
= 10;
String info
= "尚硅谷";
int[] scores
= new int[]{56,54,87,98,77,68};
int ages
[] = {56,54,87,98,77,68};
String
[] names
= {"马龙飞","石磊","陈自强"};
String
[] foods
= new String[5];
如何访问数组的元素
foods
[0] = "宫保鸡丁";
foods
[4] = "水煮鱼";
System
.out
.println(foods
[0]);
System
.out
.println(scores
[2]);
数组的长度
System
.out
.println(names
.length
);
System
.out
.println(scores
.length
);
数组的遍历
for(int i
= 0;i
< scores
.length
;i
++){
System
.out
.println(scores
[i
]);
}
for (int i
= 0; i
< names
.length
; i
++) {
System
.out
.println(names
[i
]);
}
数组元素的默认初始化值 见《ArrayTest1.java》
数组元素的默认初始化值 (以动态初始化为例说明)
*
* ① 如果数组元素是整型(byte\short\int\long),则默认值为:0
* ② 如果数组元素是浮点型(float \ double),则默认值为:0.0
* ③ 如果数组元素是boolean型,则默认值为:false
* ④ 如果数组元素是char型,则默认值为:0 或 '\u0000'
*
* ⑤ 如果数组元素是引用数据类型,则默认值为:null
数组的内存解析
知识点3:二维数组的使用
* 1. 数组的分类:
* ① 按照数组元素的类型:基本数据类型元素的数组 vs 引用数据类型元素的数组
* ② 按照数组的维数: 一维数组 、 二维数组 、 三维数组 、 。。。。
*
* 2.数组,属于引用数据类型;数组的元素,既可以是基本数据类型,也可以是引用数据类型。
* 如果一维数组的元素,又是一维数组,则构成了二维数组。
基本使用
int[] scores
= new int[]{56,54,87,98,77,68};
String
[][] names
= new String[][]{{"闫宏浩","杨超宇"},{"付子","周斌","孟腾博"},{"王延彬","翟旭亮"}};
int[][] ages
= new int[4][3];
int[] nums
[] = new int[4][];
nums
[0] = new int[5];
nums
[1] = new int[6];
System
.out
.println(names
[1]);
System
.out
.println(names
[1][1]);
ages
[1][2] = 12;
System
.out
.println(names
.length
);
System
.out
.println(names
[0].length
);
for(int i
= 0;i
< names
.length
;i
++){
for(int j
= 0;j
< names
[i
].length
;j
++){
System
.out
.print(names
[i
][j
] + " ");
}
System
.out
.println();
}
二维数组元素的默认初始化值
形式1,比如:int[][] ages = new int[4][3];
* 外层元素:存储的是地址值
* 内层元素:
* ① 如果数组元素是整型(byte\short\int\long),则默认值为:0
* ② 如果数组元素是浮点型(float \ double),则默认值为:0.0
* ③ 如果数组元素是boolean型,则默认值为:false
* ④ 如果数组元素是char型,则默认值为:0 或 '\u0000'
*
* ⑤ 如果数组元素是引用数据类型,则默认值为:null
*
*
* 形式2,比如:int[][] nums = new int[4][];
* 外层元素:默认值null
* 内层元素:不存在,输出的话,会报异常:NullPointerException
public class ArrayTest1 {
public static void main(String
[] args
) {
int[][] ages
= new int[4][3];
System
.out
.println(ages
[0]);
System
.out
.println(ages
[0][0]);
String
[][] arr
= new String[4][2];
System
.out
.println(arr
[0][0]);
System
.out
.println("####################");
int[] nums
[] = new int[4][];
System
.out
.println(nums
[0]);
System
.out
.println(nums
[0][0]);
}
}
内存解析
知识点4:数组中的常见算法
1. 数组元素的赋值
比如:杨辉三角、回形数、随机生成彩票
2. 求数值型数组中元素的最大值、最小值、平均数、总和等
package com
.atguigu
.homework
;
public class Exer02 {
public static void main(String
[] args
) {
int[] arr
= new int[10];
for (int i
= 0; i
< arr
.length
; i
++) {
arr
[i
] = (int) (Math
.random() * 90) + 10;
}
System
.out
.print("获得的随机数为:");
for (int i
= 0; i
< arr
.length
; i
++) {
System
.out
.print(arr
[i
] + " ");
}
System
.out
.println();
int sum
= 0;
int max
= 0;
int mix
= 100;
double ave
;
for (int i
= 0; i
< arr
.length
; i
++) {
sum
+= arr
[i
];
}
for (int i
= 0; i
< arr
.length
; i
++) {
if (arr
[i
] > max
)
max
= arr
[i
];
}
for (int i
= 0; i
< arr
.length
; i
++) {
if (arr
[i
] < mix
)
mix
= arr
[i
];
}
ave
= (double)sum
/ arr
.length
;
System
.out
.println("最大值" + max
);
System
.out
.println("最小值" + mix
);
System
.out
.println("所有数的和" + sum
);
System
.out
.println("平均值" + ave
);
}
}
3.数组的复制、反转、查找(线性查找、二分法查找)
public class ArrayTest {
public static void main(String
[] args
) {
int[] array1
= new int[]{2, 3, 5, 7, 11, 13, 17, 19};
int[] array2
= new int[array1
.length
];
for (int i
= 0; i
< array1
.length
; i
++) {
array2
[i
] = array1
[i
];
}
for(int x
= 0,y
= array1
.length
- 1;x
< y
;x
++,y
--){
int temp
= array1
[x
];
array1
[x
] = array1
[y
];
array1
[y
] = temp
;
}
for (int i
= 0; i
< array1
.length
; i
++) {
System
.out
.print(array1
[i
] + " ");
}
int[] array3
= new int[]{2, 3, 5, 7, -11, 13, 0, 17, 19,0};
int value
= 0;
boolean flag
= true;
for(int i
= 10;i
< array3
.length
;i
++){
if(value
== array3
[i
]){
System
.out
.println("在索引为" + i
+ "的位置上找到了指定的元素");
flag
= false;
break;
}
}
if(flag
){
System
.out
.println("未找到指定的元素");
}
int[] arr3
= new int[]{-99,-54,-2,0,2,33,43,256,999};
boolean isFlag
= true;
int number
= 256;
number
= 25;
int head
= 0;
int end
= arr3
.length
- 1;
while(head
<= end
){
int middle
= (head
+ end
) / 2;
if(arr3
[middle
] == number
){
System
.out
.println("找到指定的元素,索引为:" + middle
);
isFlag
= false;
break;
}else if(arr3
[middle
] > number
){
end
= middle
- 1;
}else{
head
= middle
+ 1;
}
}
if(isFlag
){
System
.out
.println("未找打指定的元素");
}
}
}
4.数组元素的排序算法
十大排序算法
选择排序
直接选择排序、堆排序
交换排序
冒泡排序、快速排序
插入排序
直接插入排序、折半插入排序、Shell排序
归并排序
桶式排序
基数排序
冒泡排序
public class BubbleSortTest {
public static void main(String
[] args
) {
int[] arr
= new int[]{34,76,-8,0,56,-45,6,21};
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
;
}
}
}
for (int i
= 0; i
< arr
.length
; i
++) {
System
.out
.print(arr
[i
] + " ");
}
}
}
快排的实现原理
知识点5:Arrays工具类的使用
java.util.Arrays类即为操作数组的工具类,包含了用来操作数组(比如排序和搜索)的各种方法。
package com
.atguigu
.java
;
import java
.util
.Arrays
;
public class ArraysTest {
public static void main(String
[] args
) {
int[] arr1
= new int[]{1,2,3,4,5};
int[] arr2
= new int[]{1,2,3,4,5};
System
.out
.println(arr1
== arr2
);
System
.out
.println(Arrays
.equals(arr1
,arr2
));
System
.out
.println(Arrays
.toString(arr1
));
Arrays
.fill(arr1
,10);
System
.out
.println(Arrays
.toString(arr1
));
int[] arr3
= new int[]{34,76,-8,0,56,-45,6,21};
System
.out
.println(Arrays
.toString(arr3
));
System
.out
.println(Arrays
.toString(arr3
));
int index
= Arrays
.binarySearch(arr3
, 76);
if(index
>= 0){
System
.out
.println("找到了指定元素。位置为:" + index
);
}else{
System
.out
.println("没找到指定元素");
}
}
}
知识点6:数组中的常见异常
package com
.atguigu
.java
;
public class ArrayExceptionTest {
public static void main(String
[] args
) {
int[] arr
= new int[10];
String
[][] arr2
= new String[5][];
int[] arr3
= new int[10];
System
.out
.println(arr3
[0]);
String
[] arr4
= new String[10];
System
.out
.println(arr4
[0].toString());
}
}