public class ArrayDemo01 { public static void main(String[] args) { //变量类型 变量名字 =变量值 int[] nums;//1.定义数组 int num2[]; nums=new int[10];//2.分配空间,可存放10个int 类型数字 nums[0]=1; nums[1]=2; nums[2]=3; nums[3]=4; nums[4]=5; nums[5]=6; nums[6]=7; nums[7]=8; nums[8]=9; System.out.println(nums[9]);//默认值为0 int sum=0; for (int i = 0; i < nums.length; i++) { sum+=nums[i]; } System.out.println("总和为:"+sum); } }
public class ArrayDemo02 { public static void main(String[] args) { //静态初始化 int[]a={1,2,3,4};//创建+赋值 Man[] mans={new Man(),new Man()};//引用类型 System.out.println(a[0]); //动态初始化,包含默认初始化(int类型为0,string类型为null) int[]b=new int[10]; b[0]=10; b[1]=11; System.out.println(b[0]); System.out.println(b[1]); System.out.println(b[2]); for (int i = 0; i <= a.length; i++) { System.out.println(a[i]);//下标越界 } } }
数组是相同数据类型的有序集合
数组也是对象,其元素相当于对象的成员变量
数组长度确定,不可越界
public class ArrayDemo03 { public static void main(String[] args) { int[] arrays= {1,2,3,4,5}; //打印全部元素 for (int i = 0; i < arrays.length; i++) { System.out.println(arrays[i]); } System.out.println("===================="); //计算元素总和 int sum=0; for (int i = 0; i < arrays.length; i++) { sum+=arrays[i]; } System.out.println("sum=" + sum); System.out.println("===================="); //查找最大值 int max=arrays[0]; for (int i = 0; i < arrays.length; i++) { if (arrays[i]>max) max=arrays[i]; } System.out.println("max=" + max); } }
public class ArrayDemo04 { public static void main(String[] args) { int[] arrays= {1,2,3,4,5}; for (int array : arrays) {//array.for 回车 增强型for循环 System.out.println(array);//遍历打印 } printArray(arrays); int[] reverse = reverse(arrays); printArray(reverse); } //打印数组元素 public static void printArray(int [] arrays){ for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i]+" "); } } //反转数组 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 class ArrayDemo05 { public static void main(String[] args) { int[][]array={{1,2},{2,3},{3,4,5}}; printArray(array[2]); System.out.println("===================="); System.out.println(array[1][1]); System.out.println("===================="); System.out.println(array.length); System.out.println("===================="); System.out.println(array[2].length); for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.println(array[i][j]); } } } //打印数组元素 public static void printArray(int [] arrays){ for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i]+" "); } } }
public class ArrayDemo06 { public static void main(String[] args) { int[] a={1,54,65,21,32}; System.out.println(a);//[I@4554617c System.out.println(Arrays.toString(a));//打印数组 printArray(a); Arrays.sort(a);//数组排序 System.out.println(Arrays.toString(a));//打印数组 Arrays.fill(a,0);//数组填充为0 System.out.println(Arrays.toString(a));//打印数组 Arrays.fill(a,2,4,6);//数组[2,4)=[2,3]填充为6 System.out.println(Arrays.toString(a));//打印数组 } public static void printArray(int[] a){ for (int i = 0; i < a.length; i++) { if (i==0){ System.out.print("["); } if (i==a.length-1){ System.out.print(a[i]+"]"); }else{ System.out.print(a[i]+", "); } } } }
八大排序之一
public class ArrayDemo07 { public static void main(String[] args) { int [] a={1,5,645,54,44,6,48,51}; int[] sort =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++) { boolean flag=false;//(优化)通过flag标识减少无意义比较 //内层循环,比较两数交换(通过第三方) 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; flag=true;//做了交换则真,否则假,不再循环 } } if (flag==false){ break; } } return array; } }
public class ArrayDemo08 { public static void main(String[] args) { //1.创建一个二维数组 11*11 0:没有旗子 1:黑棋 2:白棋 int[][] array1=new int[11][11]; array1[1][2]=1; array1[2][3]=2; //2.输出原始数组 System.out.println("输出原始数组"); for (int[] ints : array1) { //让二维数组里所有一维列数组包含于一个一维数组中 for (int anInt : ints) { //让一纬数组里所有数包含于一个数anInt中 System.out.print(anInt+"\t");//输出该一维数组 } System.out.println(); } System.out.println("===================="); //3.转换为稀疏数组保存 //获取有效值个数 int sum=0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array1[i][j]!=0){ sum++; } } } System.out.println("有效值个数"+sum); //创建一个稀疏数组的数组 int[][] array2=new int[sum+1][3];//定义并创建空间(行,列) array2[0][0]=11; array2[0][1]=11; array2[0][2]=sum; //遍历二维数组,将非0的值存放稀疏数组中 int count=0; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1[i].length; j++) { if (array1[i][j]!=0){ count++; array2[count][0]=i; array2[count][1]=j; array2[count][2]=array1[i][j]; } } } //4.输出稀疏数组 System.out.println("稀疏数组"); for (int i = 0; i < array2.length; i++) { System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]+"\t"); } System.out.println("===================="); //5.读取稀疏数组 int[][] array3=new int[array2[0][0]][array2[0][1]];//长11宽11 //6.给其中的元素还原值 for (int i =1; i < array2.length; i++) { array3[array2[i][0]][array2[i][1]]=array2[i][2]; } //7.打印 System.out.println("输出还原数组"); for (int[] ints : array1) { for (int anInt : ints) { System.out.print(anInt+"\t"); } System.out.println(); } } }