数组的定义和使用
package jjava1; public class demo1 { public static void main(String[] args){ int []nums;//定义数组 nums=new int[10];//创建数组大小 for(int i=0;i<=9;i++){ nums[i]=i;//赋值 } int sum=0; //获取数组长度:arrays.length for(int i=0;i<nums.length;i++)sum+=nums[i];//数组计算求和 System.out.println(sum);//打印总和 } }
升级版for循环遍历数组
package jjava1; public class demo02 { public static void main(String[] argc){ int []a={1,2,3,4,5}; for(int array:a){ System.out.println(array); } } }
数组反转的操作
package jjava1; public class demo02 { public static void main(String[] argc){ int []a={1,2,3,4,5}; for(int array:a){ System.out.println(array); } int[]reverse=reverse(a); for(int i=0;i< reverse.length;i++) System.out.println(reverse[i]); } public static int[] reverse(int[] a){ int[] result=new int[a.length]; //反转的操作 for(int i=0,j=result.length-1;i<a.length;i++,j--){ result[j]=a[i]; } return result; } }
二维数组输入并传入方法打印
package jjava1; import java.util.Arrays; import java.util.Scanner; public class j1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int [][] kk=new int[3][4]; for(int i=0;i<kk.length;i++){ for(int j=0;j<kk[i].length;j++){ kk [i][j]=scanner.nextInt(); } } suu(kk); // scanner.close(); } public static void suu(int[][] cc){ for(int i=0;i<cc.length;i++){ for(int j=0;j<cc[i].length;j++){ System.out.print(cc[i][j]+"\t"); }System.out.println(); } } }
package jjava1; import java.util.Arrays; import java.util.Scanner; public class j1 { public static void main(String[] args) { j1 suu=new j1(); Scanner scanner = new Scanner(System.in); int [][] kk=new int[3][4]; for(int i=0;i<kk.length;i++){ for(int j=0;j<kk[i].length;j++){ kk [i][j]=scanner.nextInt(); } } suu(kk); // scanner.close(); } public static void suu(int[][] cc){ for(int i=0;i<cc.length;i++){ for(int j=0;j<cc[i].length;j++){ System.out.print(cc[i][j]+"\t"); }System.out.println(); } } }
创建动态数组并将其从大到小排序
package step5; import java.util.Arrays; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //动态创建数组 int[] arr = new int[sc.nextInt()]; for(int i = 0 ; i< arr.length ; i++){ arr[i] = sc.nextInt(); } /********** Begin **********/ for(int j = 1 ; j< arr.length;j++){ for (int i = 0; i < arr.length -j; i++) { if(arr[i] > arr[i+1]){ int temp = arr[i];//交换位置 arr[i] = arr[i+1]; arr[i+1] = temp; } } } System.out.println(Arrays.toString(arr)); /********** End **********/ } }
Arrays类
用Arrays.toString打印数组元素
package jjava1; import java.util.Scanner; import java.util.Arrays; public class a1 { public static void main(String[] args){ Scanner a=new Scanner(System.in); int[] aa={1,2,3,4,5}; print(1); System.out.println(Arrays.toString(aa)); } public static void print(int b){ System.out.println(b); } }
用fill进行数组填充
package jjava1; import java.util.Scanner; import java.util.Arrays; public class a1 { public static void main(String[] args){ Scanner a=new Scanner(System.in); int[] aa={1,2,3,4,5,1,2,2,5,5}; System.out.println(Arrays.toString(aa)); Arrays.fill(aa,0);//数组填充 System.out.println(Arrays.toString(aa)); } }
用fill进行部分填充
package jjava1; import java.util.Scanner; import java.util.Arrays; public class a1 { public static void main(String[] args){ Scanner a=new Scanner(System.in); int[] aa={1,2,3,4,5,1,2,2,5,5}; System.out.println(Arrays.toString(aa)); Arrays.fill(aa,2,5,0);//数组填充 System.out.println(Arrays.toString(aa)); } }
稀疏数组
package jjava1; import java.util.Scanner; import java.util.Arrays; public class a1 { public static void main(String[] args){ Scanner a=new Scanner(System.in); int[][] aa=new int[11][11]; aa[1][2]=1; aa[2][3]=1; System.out.println("输出原始的数组"); for(int[] ints:aa){ for(int anInt:ints){ System.out.print(anInt+"\t"); } System.out.println(); //kkk(int aa[][]); } //Arrays.fill(aa,2,5,0);//数组填充 //System.out.println(Arrays.toString(aa)); // System.out.println(Arrays.toString(aa)); // } //转换为稀疏数组保存 //获取有效值的个数 System.out.println("=============="); int sum=0; for(int i=0;i<11;i++){ for(int j=0;j<11;j++){ if(aa[i][j]!=0)sum++; } } System.out.println("有效值的个数:"+sum); //2.创建一个稀疏数组的数组 int[][] bb=new int[sum+1][3]; bb[0][0]=11; bb[0][1]=11; bb[0][2]=sum; //遍历二维数组,将非零的值,存放稀疏数组中 int count=0; for(int i=0;i<aa.length;i++){ for(int j=0;j<aa[i].length;j++){ if(aa[i][j]!=0){ count++; bb[count][0]=i; bb[count][1]=j; bb[count][2]=aa[i][j]; } } } //输出稀疏数组 System.out.println("稀疏数组"); for(int i=0;i<bb.length;i++){ System.out.println( bb[i][0]+"\t"+ bb[i][1]+"\t"+ bb[i][2]+"\t" ); } System.out.println("=========="); System.out.println("还原"); //1.读取稀疏数组 int[][] cc=new int[bb[0][0]][bb[0][1]]; //2.给其中的元素还原 for(int i=1;i<bb.length;i++){ cc[bb[i][0]][bb[i][1]]=bb[i][2]; } //3.打印 System.out.println("输出还原的数组"); for(int[] ints:cc){ for(int anInt:ints){ System.out.print(anInt+"\t"); }System.out.println(); } } }