第二部分:Java数组
1. Java 数组的定义
• 数组是相同类型数据的有序集合.
• 相同类型的若干个数据,按照一定先后次序排列组合而成。
• 其中,每一个数据称作一个数组元素
• 每个数组元素可以通过一个下标来访问它们.
2. Java 数组的声明
• 一维数组的声明方式有两种:
• type[] arr_name;
• type arr_name[];
• 例如:
• int[] intArrays; int intArrays[];
• Person[] pArrays;
3. 数组的优势与局限
• 数组特点:
• 其长度是确定的,空间连续。数组一旦被创建,它的大小就是不可以改变的。
• 其元素必须是相同类型,不允许出现混合类型。
• 数组中的元素可以是任何数据类型,包括基本类型和引用类型。
• 数组优缺点
• 优点:按照索引查询效率高
• 缺点:添加删除元素效率低;按照内容查询效率低(无序)
• 数组属引用类型
• length, elements of the array
4. 数组的初始化
• 动态初始化
• 数组定义与为数组元素分配空间并赋值的操作分开进行。
int a[] = null;
a = new int[3];
a[0] = 3;
a[1] = 9;
a[2] = 8;
• 静态初始化:
• 除了用new关键字来产生数组以外,还可以直接在定义数组的同时就为数组元素分配空间并赋值。
• 格式: 类型 [] 数组名 = {元素1[, 元素2 ……]};
• int [] a = {1, 2, 3, 4, 5};
public class Test {
public static void main(String args[]) {
int [] a = { 3, 5, 7 };
}
}
• 数组是引用类型,它的元素相当于类的实例变量,因此数组一经分配空间,其中的每个 元素也被按照实例变量同样的方式被隐式初始化
public class ArrayTest3 {
public static void main(String args[]) {
int a[] = new int[2];
boolean [] b = new boolean[2];
String[] s = new String[2];
for(int i = 0; i < 2; i++)
System.out.println(a[i]);
for(int i = 0; i < 2; i++)
System.out.println(b[i]);
for(int i = 0; i < 2; i++)
System.out.println(s[i]);
}
}
输出结果:
0
0
false
false
null
null
4.1 数组的界限
• 定义并用运算符new为之分配空间后,才可以引用数组中的每个元素;
• 数组元素的引用方式:arrayName[index]
• index为数组元素下标,可以是整型常量或整型表达式。如a[3] , b[i] , c[6*i];
• 数组元素下标从0开始;长度为n的数组合法下标取值范围: 0 ~ n-1;
• 每个数组都有一个属性length指明它的长度,例如:a.length 指明数组a的长度(元素个数);
• 数组的长度: 数组名.length
• 起点和终点
• 起点: 数组名[0]
• 终点: 数组名[length-1]
5. 数组的存储特点(堆和栈)
6. 数组的遍历访问(普通循环,增强for循环)
for循环:
String[] s = { "Mircosoft", "IBM", "Sun", "Oracle", "Apple" };
for (int i = 0; i < s.length; i++) {
System.out.print(s[i] + " ");
}
增强型for循环定义如下:
for(ElementType element: arrayName){};
上述for循环可被读为:for each element in arrayName do {...}
for(String str : s) {
System.out.println(str);
}
7. 数组元素的顺序查找
按顺序一位一位的查找,找到要查找数的索引(下标)。
弊端:数据量大的时候,在最差的情况下,计算机的负载也非常的大,效率低。
int[] arr = {2,4,6,8,10,12,14,16};
int num = 12;
for (int i = 0;i<arr.length;i++){
if (arr[i] == num) {
System.out.println(i);
return;
}
}
System.out.println("无");
8. 数组元素的二分法查找
优化了查找的效率。
弊端:数组必须经过排序。
private static int find() {
int[] arr = {2,4,6,8,10,12,14,16};
int num = 12;
int start = 0;
int end = arr.length-1;
int mid = (start+end)/2;
while (num != arr[mid]){
if (arr[mid] > num){
end=mid-1;
}else {
start=mid+1;
}
mid = (start+end)/2;
}
return mid;
}
9. 数组元素的选择排序
private static void sort(int[] a) {
int n = a.length;
for (int i = 1;i<=n-1;i++){
//找出最大元素的位置
int maxIndex = 0;
for (int j = 1; j <= n - i; j++) {
if (a[j] > a[maxIndex]) {
maxIndex = j;
}
}
//将最大数调到最后
int t = a[maxIndex];
a[maxIndex] = a[n - i];
a[n - i] = t;
}
}
10. 数组元素的冒泡法排序
相邻的两个数逐个的做比较,如果前一个数比后一个数小那么就交换过来,当第一轮比较完毕后最小的值一定产生在末尾,每一轮比较都会产生一个最小值
public static void bubble(int[] a){
for (int j = 1; j <= a.length - 1; j++) {
for (int i = 0; i <= a.length - j - 1; i++) {
if (a[i] > a[i + 1]) {
int t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
}
}
}
public static void write(int[] a) {
for (int i : a) {
System.out.print(i + " ");
}
}
11. Arrays工具类的使用
Java.uitl.Arrays
• 该类提供了关于数组操作的API.
• 打印数组----toString方法。
• 比较两个数组是否相同----equals方法。
• 数组排序----sort方法。
• 数组查找----binarySearch 方法
12. 二维数组的介绍
• 实质是每个元素是一维数组的一维数组
• 二维数组可以看成以数组为元素的数组。例如:
• int [][] a= {{1,2},{3,4,5,6},{7,8,9}};
• Java中多维数组的声明和初始化应按从高维到低维的顺序进行
• 例如:
int [][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[4];
a[2] = new int[3];
int t1[][] = new int[][4]; //非法
• 二维数组举例:
• int [][] a = {{1,2},{3,4,0,9},{5,6,7}};
• Java中多维数组不必须是规则矩阵形式
13. 二维数组的声明与初始化
• Declare, create and initiate in the same time :
• int intA[][] = {{1,2},{2,3},{3,4,5}};
• int intB[3][2] = {{1,2},{2,3},{4,5}};//非法
• Declare, create and initiate separately :
• int a[][] = new int[3][5];
• int b[][] = new int[3][] ;
• b[0] = new int[2];
• b[1] = new int[3];
• b[2] = new int[5];
16. 二维数组的内存分配
17. 数组的拷贝
• 使用java.lang.System类的静态方法
• public static void arraycopy (Object src,int srcPos,Object dest, int destPos,int length)
• 可以用于数组src从第srcPos项元素开始的length个元素拷贝到目标数组从destPos项开始 的length个位置。
• 如果源数据数目超过目标数组边界会抛出 IndexOutOfBoundsException 异常。
public class ArrayTest7 {
public static void main(String args[]) {
String[] s = { "Mircosoft", "IBM", "Sun", "Oracle", "Apple" };
String[] sBak = new String[6];
System.arraycopy(s, 0, sBak, 0, s.length);
for (int i = 0; i < sBak.length; i++) {
System.out.print(sBak[i] + " ");
}
System.out.println();
int[][] intArray = { { 1, 2 }, { 1, 2, 3 }, { 3, 4 } };
int[][] intArrayBak = new int[3][];
System.arraycopy(intArray, 0, intArrayBak, 0, intArray.length);
intArrayBak[2][1] = 100;
for (int i = 0; i < intArray.length; i++) {
for (int j = 0; j < intArray[i].length; j++) {
System.out.print(intArray[i][j] + " ");
}
System.out.println();
}
}
}
运行结果: