多次元配列の作成と値の読出し
まず2次元配列を二つ用意。
列数、行数が異なっても良い。
2次元配列を配列で結合、3次元化する
読出しは、2次元の添え字で1次元の配列が読み出され、3次元の添え字で値が読み出される。
<html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <meta http-equiv="Content-Style-Type" content="text/css"> <title></title> <script type="text/javascript"> <!-- var ary1 = [ [1, 1], [2, 2], [3, 3] ]; var ary2 = [ [10, 10], [20, 20] ]; var ary = [ ary1, ary2 ]; alert("ary1 = " + ary1 + " ary2 = " + ary2); alert("ary = " + ary); alert("ary[1][1] = " + ary[1][1] + " ary[1][0] = " + ary[1][0] + "\nary[0][2] = " + ary[0][2] + " ary[0][1] = " + ary[0][1] + " ary[0][0] = " + ary[0][0]); --> </script> </head> <body> <script type="text/javascript"> <!-- document.write("<p>"); for (var i = 0; i<=1; i++) { if (i == 0) { for (var j = 0; j<=2; j++) {document.write("["+i+"]["+j+"] = " + ary[i][j] + "<br />");} } document.write("<br />"); if (i == 1) { for (var j = 0; j<=1; j++) {document.write("["+i+"]["+j+"] = " + ary[i][j] + "<br />");} } } document.write("<br />"); document.write("<p>"); for (var i = 0; i<=1; i++) { if (i == 0) { for (var j = 0; j<=2; j++) { for (var k = 0; k<=1; k++) { document.write("["+i+"]["+j+"]["+k+"] = " + ary[i][j][k] + "<br />");} } } document.write("<br />"); if (i == 1) { for (var j = 0; j<=1; j++) { for (var k = 0; k<=1; k++) { document.write("["+i+"]["+j+"]["+k+"] = " + ary[i][j][k] + "<br />");} } } } document.write("</p>"); --> </script> </body> </html>