Javassahan moniuloitteinen taulukko on tosiasiassa taulukko taulukoista.
/** * Matriisi parametrina * @author Vesa Lappalainen * @version 1.0, 04.03.2003 */ public class Mat2 { public static double alkioiden_summa(double mat[][]) { double summa = 0; int riv = mat.length; for (int i=0; i<riv; i++) { int sar = mat[i].length; for (int j=0; j<sar; j++) summa += mat[i][j]; } return summa; } public static void main(String[] args) { double s1,s2,s3; double yks[][] = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } }; double mat2[][] = { {1,2,3,4},{5,6,7,8} }, mat3[][] = { {1,0,0},{0,1,0},{0,0,1} }; s1 = alkioiden_summa(yks); s2 = alkioiden_summa(mat2); s3 = alkioiden_summa(mat3); System.out.println("Summat ovat " + s1 + ", " + s2 + " ja " + s3); } }