Public classmatrixaddition{ Public Static voidMain (String args[]) {inti,j,k; intA[][] =New int[3] [4]; intB[][] = {{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}}; intC[][] =New int[3] [4]; for(i = 0;i < 3;i++) for(j = 0;j < 4;j++) A[i][j]= (i + 1) * (j + 2); for(i = 0;i < a.length;i++) for(j = 0;j < a[i].length;j++) C[i][j]=a[i][j]+B[i][j]; System.out.println ("*******matrix c********"); for(i = 0;i < c.length;i++){ for(j = 0;j < c[i].length;j++) System.out.print (C[i][j]+" "); System.out.print ("\ n"); } }}
• Various uses of arrays
Public classarraydemo{Static floatAveragearray (floata[]) { floatAverage = 0; inti; for(I =0;i < a.length;i++) {Average= Average +A[i]; } returnaverage/a.length; } Public Static voidMain (string[] args) {floatAverage,a[] = {1,2,3,4,5}; Average=Averagearray (a); System.out.println ("Average =" +average); }}
• Array as the parameter of the method.
• In the formal parameter list, the parentheses after the array name cannot be omitted, and the number of elements in the list cannot be given;
• When an array masterpiece is an argument, the address is passed, that is, the argument and the parameter have the same storage unit.
Java Learning (vi)