这是本节内容的思维导图
目录
一、一维数组
1.定义和初始化数组
2.遍历数组
3.目前数组常见的异常
4.数组内容拷贝
二、二维数组
1.二维数组的定义和初始化
2.二维数组的遍历
三、总结
1.数组定义的变量都属于引用类型
2.引用只能指向一个对象
3.地址及内存回收问题
(1)只定义不初始化
int[] arr = new int[10];
(2)定义并初始化
int[] arr = {1,2,3,4,5};
int[] arr = new int[]{1,2,3,4,5};
(3)总结点
//int[10] arr = new int[];错误
(1)for循环
int[] arr1 = {5,2,0,1,3,1,4};
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i]+" ");
}
(2)for-each(加强for循环)
int[] arr1 = {5,2,0,1,3,1,4};
for (int Y:arr1) {//int Y的意思是用数组元素的类型去定义一个变量Y,Y中存放数组的内容
System.out.print(Y+" ");
}
(3)使用方法打印
int[] arr2 = {9,8,7};
String ret = Arrays.toString(arr2);
System.out.println(ret);
(1)数组越界异常
当访问个数大于数组个数时,会报一个数组越界异常
(2)空指针异常
当数组传参的时候,如果不对参数判断(判断数组是否为空)就使用,很有可能触发该异常
(1)常规拷贝法:使用for循环
int[] arr = new int[]{1,2,3,4,5};
int[] copy = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
copy[i] = arr[i];
}
System.out.println(Arrays.toString(copy));
(2) 使用Arrays.copyOf方法拷贝
int[] arr = new int[]{1,2,3,4,5};
int[] copy = new int[arr.length];
copy = Arrays.copyOf(arr,arr.length-2);//被拷贝的数组名和长度
System.out.println(Arrays.toString(copy));
(3)使用Arrays.copyOf方法拷贝并使目的数组大小扩大两
int[] arr = new int[]{1,2,3,4,5};
arr = Arrays.copyOf(arr,arr.length*2);
arr[5]=6;
arr[6]=7;
arr[7]=8;
arr[8]=9;
arr[9]=10;
System.out.println(Arrays.toString(arr));
(4)System.arraycopyOf方法
int[] dest = new int[arr.length];
System.arraycopy(arr,2,dest,0,arr.length-3);//[2,arr.length-3)55551
System.out.println(Arrays.toString(dest));
(5)克隆法:clone()
int[] arr = new int[]{1,2,3,4,5};
int[] copy1 = arr.clone();
System.out.println(Arrays.toString(copy1));
(6)多指向写法
int[] arr = new int[]{1,2,3,4,5};
int[] copy2 = arr;
System.out.println(Arrays.toString(copy2));
(7)Arrays.equals
public static void main(String[] args) {
String arr1 = "abcdef";
String arr2 = "abcd";
String arr3 = "abcdef";
boolean bool = arr1.equals(arr2);
System.out.println(bool);
bool = arr3.equals(arr1);
System.out.println(bool);
}
public static void main(String[] args) {
int[] arr1 = new int[]{1,2,3,4,5};
int[] arr2 = new int[]{1,2,3,4,5};
boolean bool = Arrays.equals(arr1,arr2);
System.out.println(bool);
}
(8)Arrays.fill
public static void main(String[] args) {
//1.Arrays.fill,数组局部填充
int[] arr = new int[10];
Arrays.fill(arr,1,5,11);//填充下标[1,5)
System.out.println(Arrays.toString(arr));
}
二维数组是一个特殊的一维数组,每个数组内容是一个一维数组。
(1)常规赋值法(最常用)
int[][] arr = {{1,2,3},{4,5,6}};
(2)只定义而不初始化
int[][] arr = new int[][]{{},{}};
(3)只定义,省略列的写法
int[][] arr = new int[2][];
(4)不规则定义方式
public static void main(String[] args) {
int[][] arr = new int[3][];
arr[0] = new int[3];//第一行有三列
arr[1] = new int[2];//第二行有两列
arr[2] = new int[4];//第三行有四列
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)两个for循环的常规写法
public static void main(String[] args) {
int[][] arr1 = new int[][]{{1,2,3},{4,5,6}};
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1[0].length; j++) {
System.out.print(arr1[i][j]+" ");
}
System.out.println();
}
}
(2)两层for-each语句
public static void main(String[] args) {
int[][] arr1 = new int[][]{{1,2,3},{4,5,6}};
for (int[] X:arr1) {
for (int Y:X) {
System.out.print(Y+" ");
}
System.out.println();
}
}
(3)使用方法打印(深度打印)
public static void main(String[] args) {
int[][] arr1 = new int[][]{{1,2,3},{4,5,6}};
String ret = Arrays.deepToString(arr1);
System.out.println(ret);
}