この記事の目次
このページは「JAVAの多次元配列」の演習問題です。「JAVAの多次元配列」の基礎を学びたい方はこちらで参考してください。
JAVAの多次元配列
1次元配列を2つ以上まとめた配列が2次元配列になります。この記事では2次元配列、多次元配列の宣言、初期化、要素追加などの基本操作、使い方を紹介します。配列に関する演習問題も提供します。
1. 2次元配列の作成
問題:キーボードから2次元配列の行数、列数、各要素に値を入力する。要素の最大値を求める。
解答例
例
package helloworld; import java.util.Arrays; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int row, column; Scanner scanner = new Scanner(System.in); System.out.print("2次元の行数を入力: "); row = scanner.nextInt(); System.out.print("2次元の列数を入力: "); column = scanner.nextInt(); // 2次元配列の宣言 int Array[][] = new int[row][column]; System.out.println("2次元配列の各要素を入力: "); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { System.out.print("Array[" + i + "]["+ j + "] = "); Array[i][j] = scanner.nextInt(); } } // 配列の要素の最大値を表す変数を初期化する: int max = Array[0][0]; // 配列の要素の最大値を求める for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { if (max < Array[i][j]) { max = Array[i][j]; } } } System.out.print("2次元配列:" + Arrays.deepToString(Array)); System.out.print("\n要素の最大値 = " + max); } }
実行結果:
2次元の行数を入力: 3 2次元の列数を入力: 2 2次元配列の各要素を入力: Array[0][0] = 1 Array[0][1] = 2 Array[1][0] = 3 Array[1][1] = 4 Array[2][0] = 3 Array[2][1] = 6 2次元配列:[[1, 2], [3, 4], [3, 6]] 要素の最大値 = 6
2. 行列の掛け算
問題:行列Aと行列Bを宣言し、初期化し、行列Aと行列Bの掛け算をする。
解答例
例
package helloworld; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int m1, n1; //行列Aの行数と列数の宣言 int m2, n2; //行列Bの行数と列数の宣言 Scanner scanner = new Scanner(System.in); System.out.print("行列Aの行数: "); m1 = scanner.nextInt(); System.out.print("行列Aの列数: "); n1 = scanner.nextInt(); System.out.print("行列Bの行数: "); m2 = scanner.nextInt(); System.out.print("行列Bの列数: "); n2 = scanner.nextInt(); int[][] A = new int[m1][n1]; int[][] B = new int[m2][n2]; // 行列 C = A * Bを宣言する int C[][] = new int[m1][n2]; while (n1 != m2) { System.out.println("行列を掛け算するために、「行列Aの列数=行列Bの行数」にならなければなりません。"); System.out.print("行列Aの列数: "); n1 = scanner.nextInt(); System.out.print("行列Bの行数: "); m2 = scanner.nextInt(); } // A行列の要素を入力する。 System.out.println("A行列の要素を入力: "); for (int i = 0; i < m1; i++) { for (int j = 0; j < n1; j++) { System.out.print("A[" + i + "]["+ j + "] = "); A[i][j] = scanner.nextInt(); } } //B行列の要素を入力する。 System.out.println("B行列の要素を入力: "); for (int i = 0; i < m2; i++) { for (int j = 0; j < n2; j++) { System.out.print("B[" + i + "]["+ j + "] = "); B[i][j] = scanner.nextInt(); } } // A,B行列を表示する System.out.println("A行列: "); for (int i = 0; i < m1; i++) { for (int j = 0; j < n1; j++) { System.out.print(A[i][j] + "\t"); } System.out.println("\n"); } System.out.println("B行列: "); for (int i = 0; i < m2; i++) { for (int j = 0; j < n2; j++) { System.out.print(B[i][j] + "\t"); } System.out.println("\n"); } // C行列 = A * Bを計算する for (int i = 0; i < m1; i++) { for (int j = 0; j < n2; j++) { C[i][j] = 0; for (int k = 0; k < n1; k++) { C[i][j] = C[i][j] + A[i][k] * B[k][j]; } } } // C行列の表示 System.out.println("C行列の表示: "); for (int i = 0; i < m1; i++) { for (int j = 0; j < n2; j++) { System.out.print(C[i][j] + "\t"); } System.out.println("\n"); } } }
実行結果:
行列Aの行数: 2 行列Aの列数: 3 行列Bの行数: 3 行列Bの列数: 2 A行列の要素を入力: A[0][0] = 2 A[0][1] = 3 A[0][2] = 1 A[1][0] = 4 A[1][1] = 5 A[1][2] = 2 B行列の要素を入力: B[0][0] = 3 B[0][1] = 1 B[1][0] = 2 B[1][1] = 5 B[2][0] = 2 B[2][1] = 1 A行列: 2 3 1 4 5 2 B行列: 3 1 2 5 2 1 C行列の表示: 14 18 26 31
広告