7. 多次元配列
7. 多次元配列
多次元配列の作成
lesson 017
class Lesson017 { public static void main(String args[]) { int ary[][] = new int[2][3]; ary[0][0] = 10; ary[0][1] = 20; ary[0][2] = 30; ary[1][0] = 40; ary[1][1] = 50; ary[1][2] = 60; System.out.println("配列0,xの和 = " + (ary[0][0] + ary[0][1] + ary[0][2])); System.out.println("配列1,xの和 = " + (ary[1][0] + ary[1][1] + ary[1][2])); } }
実行結果
L:\>java Lesson017
配列0,xの和 = 60
配列1,xの和 = 150
多次元配列の初期化
lesson 018
class Lesson018 { public static void main(String args[]) { int ary[][] = { {10,20} , {30,40} , {50 , 60} }; System.out.println(ary[0][0]); System.out.println(ary[0][1]); System.out.println(ary[1][0]); System.out.println(ary[1][1]); System.out.println(ary[2][0]); System.out.println(ary[2][1]); } }
実行結果
L:\>java Lesson018
10
20
30
40
50
60
多次元配列の要素数
lesson 019
class Lesson019 { public static void main(String args[]) { int ary[][] = { {10} , {30,40} , {50 , 60 , 70 , 80} }; System.out.println("aryの要素数 = " + ary.length); System.out.println("ary[0]の要素数 = " + ary[0].length); System.out.println("ary[1]の要素数 = " + ary[1].length); System.out.println("ary[2]の要素数 = " + ary[2].length); } }
実行結果
L:\>java Lesson019
aryの要素数 = 3
ary[0]の要素数 = 1
ary[1]の要素数 = 2
ary[2]の要素数 = 4