一.一维数组
1.定义数组的三种方式
int[] array = {1,2,3,4,5}; int[] array2= new int[]{1,2,3,4,5}; int[] array3= new int [5];
array是一个引用变量,创建在栈上,存放的是堆上的一个地址
如果没有初始化,默认值是0
boolean类型的默认值是false
2.遍历数组的三种方法
1.下标遍历
public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5}; for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } System.out.println(); } }
2.for each 遍历(加强for循环)
public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5}; for (int x:array){ System.out.print(x+" "); } } }
3.Array.toString遍历(需要导包,输出自动补中括号)
public class TestDemo { public static void main(String[] args) { int[] array = {1,2,3,4,5}; String ret = Arrays.toString(array); System.out.println(ret); } }
三种输出结果
3.数组是引用类型
1.引用指向(引用)一个对象的内存分布
4.array1=array2
其意思就是把array2的值赋给array1,因为array2的值是其指向的地址,所以array1现在的值变成了array2所指向的地址,则array1与array2共同指向这一块地址。
当没有人引用array1这个对象时,其就会被JVM的垃圾回收器回收了。
5.思考题
下面这段代码会输出什么??
public class TestDemo { public static void fuc1(int[]array){ array = new int[]{1,2,3}; } public static void fuc2(int[]array){ array[0]=99; } public static void main(String[] args) { int[] array={5,6,7}; fuc1(array); for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } System.out.println(); fuc2(array); for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } System.out.println(); } }
6.数组作为返回值
public class TestDemo { public static int[] test(){ int[] array = {1,2,3,4,5,6}; return array; } public static void main(String[] args) { int[] ret = test(); System.out.println(Arrays.toString(ret)); } }
虽然test3的栈帧销毁了,但此时tmp引用的对象不被销毁,因为ret对他引用了,只要对象有被引用,就不会被销毁。
7.实现数组转字符串函数
public static String my_toString(int[] array){ String ret = "["; for (int i = 0; i < array.length; i++) { ret +=array[i]; if(i==array.length - 1) break; ret +=","; } ret+="]"; return ret; }
8.数组拷贝的三种方式
1.for
public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = new int[array.length]; for (int i = 0; i < array.length; i++) { copy[i] = array[i]; } System.out.println(Arrays.toString(copy)); }
2.copyOf
public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = Arrays.copyOf(array,6); System.out.println(Arrays.toString(copy)); }
3.copyOfRange
public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = Arrays.copyOfRange(array,0,6);//左闭右开 System.out.println(Arrays.toString(copy)); }
4.arraycopy
public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = new int[5]; System.arraycopy(array,0,copy,0,5); System.out.println(Arrays.toString(copy)); }
这五个参数的含义
a.你要拷贝的数组
b.你要从这个数组的那个下标开始拷贝
c.你要拷贝到哪个数组
d.你要拷贝这个数组到哪个位置开始
e.你要拷贝多大
5.clone
public static void main(String[] args) { int[] array = {1,2,3,4,5,6}; int[] copy = array.clone(); System.out.println(Arrays.toString(copy)); }
9.二分查找
public static int binarySearch(int[] array,int key){ int left = 0; int right = array.length - 1; while (left <= right){ int mid = (left + right)/2; if (array[mid] == key){ return mid; }else if(array[mid]>key){ right = mid - 1; }else { left = mid + 1; } } return -1; }
10.冒泡排序
public static void bubbleSort(int[] array){ boolean flag = true; for (int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array.length - 1 - i; j++) { if (array[j] > array[j + 1]) { int tmp = array[j]; array[j] = array[j + 1]; array[j + 1] = tmp; flag = false; } } } if(flag){ return; } }
二.二维数组
1.二维数组的创建
public static void main(String[] args) { int[][] array1 = {{1,2,3},{4,5,6}}; int[][] array2 = new int[][]{{1,2,3},{4,5,6}}; int[][] array3 = new int[2][3]; }
2.打印二维数组的三种方式
1.for
public static void main(String[] args) { int[][] array = {{1,2,3},{4,5,6}}; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.println(array[i][j]+" "); } } }
2.for each
for(int[]tmp:array){ for(int x:tmp){ System.out.println(x+" "); } }
3.deepToString
System.out.println(Arrays.deepToString(array));
3.不规则数组
public static int[] test(){ int[] array = {1,2,3,4,5,6}; return array; }