Skip to content

Instantly share code, notes, and snippets.

@blueplanet
Last active August 29, 2015 14:05
Show Gist options
  • Save blueplanet/63c8929a6f1f19166650 to your computer and use it in GitHub Desktop.
Save blueplanet/63c8929a6f1f19166650 to your computer and use it in GitHub Desktop.
kadai3
package edu;
public class Kadai3 {
public static void main(String[] args) {
int origin[][] = new int[][] {{ 1, 2, 3 }, { 4, 5, 6 }};
printArray("変換前", origin);
int toSize1[] = new int[] {3, 2};
int toArray1[][] = convert(origin, toSize1);
printArray("3行2列に変換", origin);
// 期待結果
// 1 4
// 2 5
// 3 6
int toSize2[] = new int[] {6, 1};
int toArray2[][] = convert(origin, toSize2);
printArray("6行1列に変換", origin);
// 期待結果
// 1
// 2
// 3
// 4
// 5
// 6
}
private static int[][] convert(int origin [][], int toSize []) {
int arr2[][] = new int[atoSize[0]][tosize[1]];
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr.length; j++) {
arr2[i][j] = arr[j][i];
}
}
System.out.println("変更後:");
//(printArrayメソッドを使って)配列 arr2 を表示する
printArray(arr2);
}
private static void printArray(String title, int arr [][]) {
System.out.println(title);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment