エンジニア成長日記 swaponQ

コンピュータサイエンス専攻の一般人のブログです。

新・明解 Java 入門 演習 7-26, 7-27, 7-28, 7-29 解答

Hello, Terminal!swaponQです!

今回は演習 7-26, 7-27, 7-28, 7-29 に取り組んでいこうと思います。

  • 演習 7-26

配列の要素a[idx]にxを挿入した配列を返却するメソッドarrayInsOfを作成せよ。


ソースコード

import java.util.Scanner;

class ex07_26{
  //7-20で作成したaryInsを使用
  static void aryIns(int[] a,int idx,int x) {
    int t = a.length-1;
    for(int i = a.length-1; i>idx; i--){
      a[i] = a[i - 1];
    }
    a[idx] = x;
  }
  static int[] arrayInsOf(int[] a,int idx,int x) {
    int ans[] = new int[a.length+1];
    for(int i=0; i<a.length; i++)
    ans[i] = a[i];
    ans[a.length] = 0;
    aryIns(ans,idx,x);
    return ans;
  }
  public static void main(String[] args) {
    Scanner stdIn = new Scanner(System.in);
    System.out.print("要素数:");
    int num = stdIn.nextInt();
    int[] a = new int[num];
    for (int i = 0; i < num; i++) {
      System.out.print("a[" + i + "] : ");
      a[i] = stdIn.nextInt();
    }
    System.out.print("挿入したい要素:");
    int n = stdIn.nextInt();
    System.out.print("挿入したい場所:");
    int idx = stdIn.nextInt();
    int[] b = arrayInsOf(a,idx,n);
    for (int i = 0; i < b.length; i++)
    System.out.println("b[" + i + "] = " + b[i]);
  }
}

・実行結果

要素数:3
a[0] : 2
a[1] : 5
a[2] : 7
挿入したい要素:4
挿入したい場所:1
b[0] = 2
b[1] = 4
b[2] = 5
b[3] = 7
  • 演習 7-27

List7-20のプログラムを三つの配列の要素数が等しければ加算を行ってtrueを返し、等しくなければ加算を行わずにfalseを返すメソッドに書き換えよ。

ソースコード

class ex07_27 {
  static boolean addMatrix(int[][] x, int[][] y, int[][] z) {
    boolean flag = true;
    if((x.length == y.length) && (x.length == z.length)){
      for(int i=0; i < x.length; i++){
        if(!((x[i].length == y[i].length) && (z[i].length==y[i].length)))
        flag = false;
      }
    } else {
      flag = false;
    }
    if(flag == true){
      for (int i = 0; i < x.length; i++) {
        for (int j = 0; j < x[i].length; j++) {
          z[i][j] = x[i][j] + y[i][j];
        }
      }
    }
    return flag;
  }
  static void printMatrix(int[][] m) {
    for (int i = 0; i < m.length; i++) {
      for (int j = 0; j < m[i].length; j++)
      System.out.print(m[i][j] + "  ");
      System.out.println();
    }
  }
  public static void main(String[] args) {
    int[][] a = { {1, 2, 3}, {4, 5, 6} };
    int[][] b = { {6, 3, 4}, {5, 1, 2} };
    int[][] c = new int[2][3];
    boolean pflag = addMatrix(a, b, c);
    if (pflag == true) {
      System.out.println("行列a");printMatrix(a);
      System.out.println("行列b");printMatrix(b);
      System.out.println("行列c");printMatrix(c);
    } else {
      System.out.println("false");
    }
  }
}

・実行結果

行列a
1  2  3
4  5  6
行列b
6  3  4
5  1  2
行列c
7  5  7
9  6  8
  • 演習 7-28

行列xとyの和を格納した2次元配列を返すメソッドを作成せよ。

ソースコード

class ex07_28 {
  static int[][] addMatrix(int[][] x, int[][] y) {
    int[][]z = new int[x.length][x[0].length];
    for (int i = 0; i < x.length; i++) {
      for (int j = 0; j < x[i].length; j++) {
        z[i][j] = x[i][j] + y[i][j];
      }
    }
    return z;
  }
  static void printMatrix(int[][] m) {
    for (int i = 0; i < m.length; i++) {
      for (int j = 0; j < m[i].length; j++)
      System.out.print(m[i][j] + "  ");
      System.out.println();
    }
  }
  public static void main(String[] args) {
    int[][] a = { {1, 2, 3}, {4, 5, 6} };
    int[][] b = { {6, 3, 4}, {5, 1, 2} };
    int[][] c = addMatrix(a,b);
    System.out.println("行列a");printMatrix(a);
    System.out.println("行列b");printMatrix(b);
    System.out.println("行列c");printMatrix(c);
  }
}

・実行結果

行列a
1  2  3
4  5  6
行列b
6  3  4
5  1  2
行列c
7  5  7
9  6  8
  • 演習 7-29

2次元配列aと同じ配列(要素数が同じで、すべての要素の値が同じ配列)を生成して返却するメソッドaryClone2を作成せよ。

ソースコード

class ex07_29 {
  static int[][] aryClone2(int[][] a) {
    int[][] ans = new int[a.length][a[0].length];
    for (int i = 0; i<ans.length; i++) {
      for (int j = 0; j<ans[i].length; j++) {
        ans[i][j] = a[i][j];
      }
    }
    return ans;
  }
  static void printMatrix(int[][] m) {
    for (int i = 0; i < m.length; i++) {
      for (int j = 0; j < m[i].length; j++)
      System.out.print(m[i][j] + "  ");
      System.out.println();
    }
  }
  public static void main(String[] args) {
    int[][] a = {{1, 2, 3}, {4, 5, 6}};
    int[][] b = aryClone2(a);
    System.out.println("行列a");printMatrix(a);
    System.out.println("行列b");printMatrix(b);
  }
}

・実行結果

行列a
1  2  3
4  5  6
行列b
1  2  3
4  5  6

今回は以上です。お疲れ様でした!
次回は演習 7-30, 7-31, 7-32, 7-33 です。

Goodbye, Terminal… swaponQでした!