public class MatrixAddition{ public static void main(String args[]){ int i,j,k; int a[][] = new int [3][4]; int b[][] = {{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}}; int c[][] = 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(" "); } } }
·数组的各种运用
public class ArrayDemo{ static float AverageArray(float a[]){ float average = 0; int i; for(i =0;i < a.length;i++){ average = average + a[i]; } return average/a.length; } public static void main(String[] args){ float average,a[] = {1,2,3,4,5}; average = AverageArray(a); System.out.println("average = "+average); } }
·数组作为方法的参数。
·形参列表中,数组名后的括号不能省略,可以不给出数组元素的个数;实参列表中,数组名后面不需要括号。
·数组名作为实参时,传递的是地址,即实参和形参具有相同的存储单元。