dataType[] arrayRefVar; //首选方法 //or dataType arrayRefVar[];//效果相同
例子:
int[] nums; //声明一个数组 //or int nums[];
dataType[] arrayRefVar = new dataType[arraySize];
例子:
nums = new int[5];//创建一个数组([10]为分配的大小) int[] nums = new int[5]; //声明创建写在一起
数组的元素是通过索引访问的,数组索引从0开始。
nums[0] = 1; nums[1] = 2; nums[2] = 3; //.....
获取数组长度:
arrays.length
例子:
int[] nums = new int[5]; //计算所有元素的和 nums[0] = 1; nums[1] = 2; nums[2] = 3; nums[3] = 4; nums[4] = 5; int sum = 0; //获取数组长度 for (int i = 0; i < nums.length; i++){ //快捷fori+Alt+回车 sum = sum + nums[i]; } System.out.println("总和为:" + sum);
下标的合法区间:[0, length-1],如果越界就会报错
public static void main(String[] args)}{ int[] a = new int[2]; System.out.println(a[2]); }
For循环
int[] arrays = {1, 2, 3, 4, 5}; int sum = 0; for (int i = 0; i < arrays.length; i++){ sum += arrays[i]; } System.out.println("和="+sum);
For-Each循环
int[] arrays = {1,2,3,4,5}; for (int array : arrays){ //(arrays.for)没有下标 System.out.println(array);//输出所有元素 }
数组作方法入参
public class Demo { public static void main(String[] args) { int[] arrays = {1,2,3,4,5}; printArray(arrays); } //打印数组元素的方法 public static void printArray(int[] arrays){ for(int i = 0; i < arrays.length; i++){ System.out.print(arrays[i]+" "); } } }
数组作返回值
public class Demo { public static void main(String[] args) { int[] arrays = {1,2,3,4,5}; int[] reverse = reverse(arrays); //返回一个结果,已经反转过 printArray(reverse); } //反转数组 public static int[] reverse(int[] arrays){ int[] result = new int[arrays.length]; //反转的语句 for (int i = 0; j = result.length-1; i < arrays.length; i++,j--){ result[j] = arrays[i]; } return result; } //打印数组元素的方法 public static void printArray(int[] arrays){ for(int i = 0; i < arrays.length; i++){ System.out.print(arrays[i]+" "); } } }
int a[][] = new int[2][5];
例子:
int [][]array = {{1,2},{2,3},{3,4},{4,5}}; System.out.println(array[0][0]);//第0个数组中第0个数组元素,1。 printArray(array[0]);//打印第一组元素中所有数组元素,1和2.用System.out.println()无法打印出,需配合打印方法。 System.out.println(array.length);//输出4,为外围数组长度。 System.out.println(array[0].length);//输出4和2。 //打印数组元素方法 public static void printArray(int[] arrays){ for(int i = 0; i < arrays.length; i++){ System.out.print(arrays[i]+" "); } }
例子:
int[] a = {1,8,43,54,12,34}; System.out.println(a); //[I@1b6d3586 //打印数组元素 System.out.println(Arrays.toString(a));//Arrays.toString为工具类,输出[1, 8, 43, 54, 12, 34] //数组排序 Arrays.sort(a); System.out.println(Arrays.toString(a));//输出为[1, 8, 12, 34, 43, 54]。 //数组填充 Arrays.fill(a,2,4,0);//(a,formlndex:2,tolendex:4,val:0) System.out.println(Arrays.toString(a));//2~4下标之间的数字被零填充,输出[1, 8, 0, 0, 43, 54],包括下标3.
例子:
public class Demo{ public static void main(String[] args){ int[] a = {8,1,54,87,34,12}; int[] sort = sort(a);//sort(a)调用排序方法,返回一个排序后的数组。 System.out.println(Arrays.toString(sort)); } //冒泡排序 public static int[] sort(int[] array){ int temp = 0;//临时变量 //外层循环,判断循环多少次 for (int i = 0; i < array.length-1; i++){ //内层循环,比较大小,交换位置 for (int j = 0; j < array.length-1-i; j++){ if (array[j+1] < array[j]){ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } return array; } }
优化例子:
public class Demo{ public static void main(String[] args){ int[] a = {1,8,43,54,12,34}; int[] sort = sort(a); System.out.println(Arrays.toString(sort)); } public static void sort(int[] array){ int temp = 0; for (int i = 0; i < array.length-1; i++){ boolean flag = false;//通过flag标识位减少没有意义的比较 for (int j = 0; j < array.lenth-1-i; j++){ if (array[j+1] < array[j]){ temp = array[j]; array[j] = array[j+1]; arrat[j+1] = temp; flag = ture; } } if (flag == false){ break; } } return array; } }
public class Demo{ public static void main(String[] args) { //1.创建一个二维数组 11*11 int[][] array = new int[11][11]; array[1][2] = 1; array[2][3] = 2; //输出原始的数组 System.out.println("输出原始数组"); for (int[] ints : array) { for (int anInt : ints) { System.out.print(anInt + "\t"); } System.out.println(); } //转换为稀疏数组保存 //获取有效值个数 int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array[i][j] != 0) { sum++; } } } System.out.println("有效值的个数:" + sum); //创建一个稀疏数组 int[][] array01 = new int[sum + 1][3]; array01[0][0] = 11; array01[0][1] = 11; array01[0][2] = sum; //遍历二维数组,将非零的值存放在稀疏数组中 int count = 0; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { if (array[i][j] != 0) { count++; array01[count][0] = i; array01[count][1] = j; array01[count][2] = array[i][j]; } } } System.out.println("============================"); //输出稀疏数组 System.out.println("稀疏数组"); for (int i = 0; i < array01.length; i++) { System.out.println(array01[i][0] + "\t" + array01[i][1] + "\t" + array01[i][2] + "\t"); } System.out.println("============================"); //还原 System.out.println("还原"); //读取稀疏数组的值 int[][] array02 = new int[array01[0][0]][array01[0][1]]; //给其中的元素还原它的值 for (int i = 1; i < array01.length; i++){ array02[array01[i][0]][array01[i][1]] = array01[i][2]; } //打印 System.out.println("输出还原数组"); for (int[] ints : array02) { for (int anInt : ints) { System.out.print(anInt + "\t"); } System.out.println(); } } }
输出结果:
输出原始数组 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 有效值的个数:2 ============================ 稀疏数组 11 11 2 1 2 1 2 3 2 ============================ 还原 输出还原数组 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 进程已结束,退出代码为 0
堆:
栈:
静态初始化
int[] a = {1,2,3}; //直接初始化(创建+赋值),放多少个数组元素就有多大,一旦定义就不可改变 System.out.println(a[0]);//输出1 Num[] nums = {new Num(1,1), new Num(2,2)};//引用其他类(Num)中的数组元素
动态初始化(包含默认初始化)
int[] a = new int[2];// 声明数组,创建数组 //赋值(若未赋值,则默认为0) a[0] = 1; a[1] = 2;
数组的默认初始化