Packagecom.xxx.xxx;Importjava.util.Arrays; Public classDemo8 {/*** 1. Initialize one-dimensional array using one-dimensional array * 2, initialize two-dimensional array using two-dimensional array * 3, fill replace array element *@paramargs*/ Public Static voidMain (string[] args) {//1, the first type of initialization intArr[] =New int[]{1,2,3,4,5,6};//one-dimensional arrays//1.1, the second method of initialization intArr2[] = {1,0,2,3,4}; //1.2. For loop output for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+","); } System.out.println (); //For each output for(inta1:arr2) {System.out.print (A1+","); } System.out.println ("\ n------------------------------"); //2. Initialization of two-dimensional arrays intMyarr[][];//First KindMyarr =New int[2] [4]; //2.1, output two-dimensional array valuesSystem.out.println ("myarr[] Value:"); for(inti=0;i<myarr.length;i++){ for(intj=0;j<myarr[i].length;j++) {//iterate through each element of an arraySystem.out.print (Myarr[i][j]);//Output} System.out.println (); //output white space line wrapping} System.out.println ("--------------------\ n"); //The second Kind inta1[][]; A1=New int[2][]; a1[0] =New int[2]; a1[1] =New int[3]; //For each outputSYSTEM.OUT.PRINTLN ("The element in the array is:");//Prompt Information inta[][]={{4,3},{1,2},{0,1},{2,3}};//defining a two-dimensional array for(intX[]: a) {//the outer loop variable is a one-dimensional array for(intE:X) {//iterate through each array element if(E = = a.length) {//determine if a variable is the last element in a two-dimensional arraySystem.out.println (e);//output The last element of a two-dimensional array}Else{ //if it is not the last element in a two-dimensional arraySystem.out.print (e+ ","); } } } intArr3[][] = {{12,0},{45,10}};//Third KindARR3[1][1] = 20; //3. Fill and replace array arrays.fill (int[] a,int value)//Method 1 intArr5[] =New int[5];//Create an int arrayArrays.fill (ARR5, 8);//use the same value to populate an array for(inti=0;i<arr5.length;i++){ //outputs an element in an array at a timeSystem.out.println ("\ n" +i+ "elements are:" +Arr5[i]); } //Method 2 /*** Fill (int[] a,int fromindex,int toindex,int value) * A to populate the array * FromIndex to The index of the first element that is populated with the specified value (including) where to start * toindex the number of digits to fill with the index of the last element to fill with the specified value Eg:toindex-fromindex * Value to store in all elements of the array*/ intArr6[] =New int[]{45,12,2,10}; //populate 3-1=2 from the second bit of the array, the output array results in 8 8Arrays.fill (ARR6, 1, 3, 8);//Use the Fill () method to initialize an array for(inti=0;i< arr6.length;i++) {System.out.println ("\ n---------------------\ arr6[]" +i+ "element is:" +Arr6[i]); } }}
7, one-dimensional array and two-dimensional array ear creation, output, Arrays.fill () replacement