概念:
用来存储一组相同数据类型的集合(容器)
引用数据类型:类 接口 数组
特点:
数组中的元素类型必须一致数组本身是引用数据类型,但是里面的元素既可以是基本数据类型,也可以是引用数据类型数组的长度一旦给定便无法改变声明:
//数据类型[] 数组名; int[] arr; //数据类型 数组名[]; String arr[];main方法中 args 和 [] 位置可以更改
格式:
//数据类型[] 数组名 = new 数据类型[长度]; int[] arr2 = new int[4];数据的存取:
索引值:数组中元素的序号(从0开始,最大值是数组的长度减1)赋值: 数组名[索引值] = 要赋的值 取值: 数组名[索引值]; public class Demo1 { public static void main(String[] args) { String arr[] = new String[4]; arr[0] = "春季"; arr[1] = "夏季"; arr[2] = "秋季"; arr[3] = "冬季"; System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); System.out.println(arr[3]); char arr1[] = new char[4]; arr1[0] = '多'; arr1[1] = '易'; arr1[2] = '教'; arr1[3] = '育'; System.out.println(arr1[0]); System.out.println(arr1[1]); System.out.println(arr1[2]); System.out.println(arr1[3]); int arr2[] = new int[10]; for (int i = 0; i < 10; i++) { arr2[i] = i + 1; System.out.println(arr2[i]); } } }如果堆中没有栈中的变量指向时,就会成为垃圾,等待GC自动回收
public class Demo2 { public static void main(String[] args) { int[] arr = new int[3]; System.out.println(arr);//打印数组在栈中的地址值 System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); arr[1] = 100; System.out.println(arr[1]); } } public class Demo3 { public static void main(String[] args) { String[] arr1 = new String[5]; arr1[1] = "大"; System.out.println(arr1[1]); String[] arr2 = new String[4]; arr2[1] = "小"; arr2 = arr1;//将arr1的地址值赋值给arr2 System.out.println(arr1[1]);//大 System.out.println(arr2[1]);//大 } }格式:
//数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3…} char[] ch = new char[] { '春', '夏', '秋', '冬' }; //数据类型[] 数组名 = {元素1,元素2,元素3…} boolean[] bool = { false, false, true, true };第二种类型只有初始化时可以定义,重新赋值不可以这样写
获取数组的长度:
数组名.length public class Dem4 { public static void main(String[] args) { char[] ch = new char[] {'春', '夏', '秋','冬' }; for (int i = 0; i < ch.length; i++) { System.out.println(ch[i]); } boolean[] bool = { false, false, true, true }; for (int i = 0; i < bool.length; i++) { System.out.println(bool[i]); } int[] num = { 4, 8, 8, 9, 5 }; int sum = 0; double pingjun = 0; for (int i = 0; i < num.length; i++) { sum += num[i]; pingjun = (double)sum / num.length ; } System.out.println("五个数之和为:" + sum); System.out.println("五个数的平均值为:" + pingjun); } }格式:
/*数据类型[][] 数组名 = new 数据类型[m][n]; *m:二维数组中一维数组的个数 *n:一维数组中的元素个数*/ int[][] arr = new int[5][4] ; /*数据类型[][] 数组名 = new 数据类型[m][]; *m: 二维数组中一维数组的个数*/ int[][] arr1 = new int[3][]; /*数据类型[][] 数组名 = new 数据类型[][]{{元素1,元素2...},{元素1,元素2...},{元素1,元素2...}...}; 简写: 数据类型[][] 数组名 = {{元素1,元素2...},{元素1,元素2...},{元素1,元素2...}...};*/ int[][] arr2 = {{1,2,3,4,5,6},{7,8,9},{10,20,30}}; public class Demo7 { public static void main(String[] args) { int[][] arr = new int[5][4] ; arr[1][1] = 5; arr[4][3] = 10; 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(); } int[][] arr1 = new int[3][]; arr1[0] = new int[] {1,2,3,4,5}; arr1[1] = new int[] {100,200,300}; arr1[2] = new int[] {66,88}; for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr1[i].length; j++) { System.out.print(arr1[i][j]+" "); } System.out.println(); int[][] arr2 = {{1,2,3,4,5,6},{7,8,9},{10,20,30}}; arr[0][2] = 10; for (int k = 0; k < arr2.length; k++) { for (int l = 0; l < arr2[k].length; l++) { System.out.print(arr2[k][l]); } System.out.println(); } } } }