□ 宣言と生成 int[] a; a = new int[10]; または int[] a = new int[10]; □ 参照 a[0] = 0; a[1] = 1; : a[9] = 45; a.length = 10 <-- 配列aの要素数 |
図.基本データ型の配列 |
□ 初期値の設定 int[] a = {0, 10, 20}; または int[] a; a = new int[] {0, 10, 20}; |
図.基本データ型の配列 |
- public class Days10 {
- public static void main(String[] args) {
- int month = Integer.parseInt(args[0]); // コマンドラインからの引数をint型に変換します。
- // 月 1 2 3 4 5 6 7 8 9 10 11 12
- int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 日数
- // 索引 0 1 2 3 4 5 6 7 8 9 10 11
- System.out.println(month + " 月は " + days[month - 1] + " 日です。");
- }
- }
□ 実行結果
$ java Days10 2 2 月は 28 日です。
- public class Array51 {
- public static void main(String[] args) {
- int[] a = new int[10];
- for (int i = 0; i < a.length; i++) {
- System.out.println("a[" + i + "] = " + a[i]);
- }
- }
- }
□ 実行結果
$java Array51 a[0] = 0 a[1] = 0 a[2] = 0 a[3] = 0 a[4] = 0 a[5] = 0 a[6] = 0 a[7] = 0 a[8] = 0 a[9] = 0
データ型 | 初期値 |
---|---|
byte, short, int, long | 0 |
float | 0.0F |
double | 0.0 |
char | '\u0000' |
boolean | false |
参照型 | null |
- public class ArrayUtil {
- static String toString(int[] a) {
- StringBuffer sb = new StringBuffer("[");
- for (int i = 0; i < a.length; i++) {
- if (i != a.length -1) {
- sb.append(a[i] + ", ");
- } else {
- sb.append(a[i]);
- }
- }
- sb.append("]");
- return sb.toString();
- }
- }
- public class Array52 {
- public static void main(String[] args) {
- int[] a = new int[10]; // 要素10個のint型配列の宣言と生成
- System.out.println(ArrayUtil.toString(a)); // 配列を文字列化して表示します。
- }
- }
□ 実行結果
$java Array52 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- public class Array71 {
- public static void main(String[] args) {
- int[] a = new int[4];
- a[0] = 100;
- a[1] = -100;
- a[2] = 0;
- a[3] = 0xffff;
- System.out.println(ArrayUtil.toString(a));
- }
- }
□ 実行結果
$java Array71 [100, -100, 0, 65535]
- public class Array72 {
- public static void main(String[] args) {
- int[] a = {100, -100, 0, 0xffff};
- System.out.println(ArrayUtil.toString(a));
- }
- }
□ 実行結果
$java Array72 [100, -100, 0, 65535]
- public class Sum11 {
- public static void main(String[] args) {
- int[] a = {100, -100, 0, 0xff, -077};
- // ここにコードを記述してください。
- System.out.println("a[] = " + ArrayUtil.toString(a));
- System.out.println("sum = " + sum);
- }
- }
□ 実行結果
$ java Sum11 a[] = [100, -100, 0, 255, -63] sum = 192
- public class MinMax11 {
- public static void main(String[] args) {
- int[] a = {100, -100, 0, 0xff, -077};
- // ここにコードを記述してください。
- System.out.println("a[] = " + ArrayUtil.toString(a));
- System.out.println("min = " + min + ", max = " + max);
- }
- }
□ 実行結果
$ java MinMax11 a[] = [100, -100, 0, 255, -63] min = -100, max = 255
- public class MinMax12 {
- public static void main(String[] args) {
- int[] a = new int[0];
- // ここにコードを記述してください。
- System.out.println("a[] = " + ArrayUtil.toString(a));
- System.out.println("min = " + min + ", max = " + max);
- }
- }
□ 実行結果
$ java MinMax12 a[] = [] min = 2147483647, max = -2147483648 a[] = [100] min = 100, max = 100 a[] = [100, -100, 0, 255, -63] min = -100, max = 255
- public class Array53 {
- public static void main(String[] args) {
- int[] a = new int[10];
- for (int i = 0; i < a.length; i++) {
- a[i] = i; // 配列要素に値を設定
- }
- System.out.println(ArrayUtil.toString(a));
- }
- }
□ 実行結果
$java Array53 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- public class ArgsPrint01 {
- public static void main(String[] args) {
- System.out.println("args.length = " + args.length);
- for (int i = 0; i < args.length; i++) {
- System.out.println(i + " : " + args[i]);
- }
- }
- }
□ 実行結果
$ java ArgsPrint01 args.length = 0 $ java ArgsPrint01 Hello Java World args.length = 3 0 : Hello 1 : Java 2 : World $ java ArgsPrint01 "Hello Java World" args.length = 1 0 : Hello Java World
- public class Array54 {
- public static void main(String[] args) {
- int[] a = {2, 9, 6, 3};
- int[] b = a; // コピー
- System.out.println("a = " + ArrayUtil.toString(a));
- System.out.println("b = " + ArrayUtil.toString(b));
- a[0] = 10;
- b[0] = 20;
- System.out.println("----");
- System.out.println("a = " + ArrayUtil.toString(a));
- System.out.println("b = " + ArrayUtil.toString(b));
- }
- }
□ 実行結果
$ java Array54 a = [2, 9, 6, 3] b = [2, 9, 6, 3] ---- a = [20, 9, 6, 3] b = [20, 9, 6, 3]Top
- public class Array55 {
- public static void main(String[] args) {
- int[] a = {2, 9, 6, 3};
- int[] b = new int[a.length];
- for (int i = 0; i < a.length; i++) {
- b[i] = a[i];
- }
- System.out.println("a = " + ArrayUtil.toString(a));
- System.out.println("b = " + ArrayUtil.toString(b));
- a[0] = 10;
- b[0] = 20;
- System.out.println("----");
- System.out.println("a = " + ArrayUtil.toString(a));
- System.out.println("b = " + ArrayUtil.toString(b));
- }
- }
□ 実行結果
$ java Array55 a = [2, 9, 6, 3] b = [2, 9, 6, 3] ---- a = [10, 9, 6, 3] b = [20, 9, 6, 3]Top
- public class Array31 {
- public static void main(String[] args) {
- int[] a = new int[10];
- for (int i = 0; i < a.length; i++) {
- a[i] = i;
- }
- System.out.println("a[] = " + ArrayUtil.toString(a));
- System.out.println();
- // 要素の正順に配列bにコピーする。
- int[] b = new int[a.length];
- // コードを記述してください。
- // 要素の逆順に配列cにコピーする。
- int[] c = new int[a.length];
- // コードを記述してください。
- // 要素を逆にする。
- int w;
- // コードを記述してください。
- // 要素を2つずらす。このとき、要素数が2つの配列は作業用として使用してもよい。
- int[] t = new int[2];
- // コードを記述してください。
- // a, b, cのプリント
- System.out.println("** a, b, cのプリント **");
- System.out.println("a[] = " + ArrayUtil.toString(a));
- System.out.println("b[] = " + ArrayUtil.toString(b));
- System.out.println("c[] = " + ArrayUtil.toString(c));
- System.out.println();
- // さらに要素を2つずらす。このとき、作業用の変数をひとつ使用してもよい。
- int u;
- // コードを記述してください。
- System.out.println("** 要素ずれのプリント **");
- System.out.println("a[] = " + ArrayUtil.toString(a));
- }
- }
□ 実行結果
$java Array31 a[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ** a, b, cのプリント ** a[] = [7, 6, 5, 4, 3, 2, 1, 0, 9, 8] b[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] c[] = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] ** 要素ずれのプリント ** a[] = [5, 4, 3, 2, 1, 0, 9, 8, 7, 6]
- // 0703
- public class ArrayUtil {
- // 途中省略
- // 乱数設定
- static int[] getRandomElement(int[] a) {
- // コードを記述してください。
- return a;
- }
- }
- public class Array73 {
- public static void main(String[] args) {
- int[] a = new int[Integer.parseInt(args[0])];
- System.out.println(ArrayUtil.toString(ArrayUtil.getRandomElement(a)));
- }
- }
□ 実行結果
$java Array73 10 [3, 3, 0, 7, 5, 8, 9, 5, 7, 4]
データ型名[][] 配列名; int[][] a;
配列名 = new データ型名[1次元目の要素数] [2次元目の要素数]; a = new int[10] [10];
データ型名[] 配列名 = new データ型名[1次元目の要素数] [2次元目の要素数]; int[] a = new int[10][10];
配列名[1次元目の添字] [2次元目の添字] a[0][1], a[0][1], ... a[0][9], a[1][0], a[1][1], ... , a[9][9]
1次元目の要素数: a.length 2次元目の要素数: a[0].length
□ 宣言と生成 int[][] a; a = new int[10][10]; または int[][] a = new int[10][10]; □ 初期値 int[][] b = {{0, 1}, {2, 3, 4}, {5, 6}}; □ 参照 a[0][0] = 0; a[0][1] = 1; : a[0][9] = 9; a[1][0] = 10; : a[9][9] = 99; a.length = 10 <-- 配列aの要素数 a[0].length = 10 <-- 配列a[0]の要素数 |
図.基本データ型の配列の配列 |
□ 初期値 int[][] b = {{0, 1}, {2, 3, 4}, {5, 6}}; |
図.基本データ型の配列の配列 |
- public class Janken11 {
- public static void main(String[] args) {
- int user = Integer.parseInt(args[0]); // コマンドラインからの引数をint型に変換します。
- int computer = (int)(Math.random() * 3); // コンピューターの手
- String[] te = {"ぐー", "ちょき", "ぱー"};
- char[][] kekka = {{'分', '勝', '負'} // ぐー
- , {'負', '分', '勝'} // ちょき
- , {'勝', '負', '分'}}; // ぱー
- //コンピューター ぐー ちょき ぱー
- System.out.print("あなた: " + te[user] + ", コンピューター: " + te[computer]);
- System.out.println(" --> 結果: " + kekka[user][computer]);
- }
- }
□ 実行結果
$ java Janken11 0 あなた: ぐー, コンピューター: ちょき --> 結果: 勝
- public class Array031 {
- public static void main(String[] args) {
- int[][] a = new int[3][3]; // 要素3*3個のint型2次元配列の宣言と生成
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a[i].length; j++) {
- a[i][j] = 10 * i + j;
- }
- }
- System.out.println(ArrayUtil.toString(a));
- }
- }
- // 0711
- public class ArrayUtil {
- // 配列の文字列化
- static String toString(int[] a) {
- StringBuffer sb = new StringBuffer("[");
- for (int i = 0; i < a.length; i++) {
- if (i != a.length -1) {
- sb.append(a[i] + ", ");
- } else {
- sb.append(a[i]);
- }
- }
- sb.append("]");
- return sb.toString();
- }
- static String toString(int[][] a) {
- StringBuffer sb = new StringBuffer("[");
- for (int i = 0; i < a.length; i++) {
- if (i != a.length -1) {
- sb.append(toString(a[i]) + ", ");
- } else {
- sb.append(toString(a[i]));
- }
- }
- sb.append("]");
- return sb.toString();
- }
- // ソート
- static int[] sort(int[] a) {
- for (int i = 0; i < a.length - 1; i++) {
- for (int j = i + 1; j < a.length; j++) {
- if (a[i] > a[j]) {
- int t = a[i];
- a[i] = a[j];
- a[j] = t;
- }
- }
- }
- return a;
- }
- // 乱数設定
- static int[] getRandomElement(int[] a) {
- for (int i = 0; i < a.length; i++) {
- a[i] = (int)(Math.random() * a.length);
- }
- return a;
- }
- }
□ 実行結果
$java Array031 [[0, 1, 2], [10, 11, 12], [20, 21, 22]]
- public class Kuku11 {
- public static void main(String[] args) {
- int[][] a = new int[9][9];
- // コードを記述してください。
- }
- }
□ 実行結果
$java Kuku11 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81
- public class Kuku12 {
- public static void main(String[] args) {
- int[][] a = new int[9][9];
- // コードを記述してください。
- }
- }
□ 実行結果
$java Kuku12 [1, 2, 3, 4, 5, 6, 7, 8, 9] [2, 4, 6, 8, 10, 12, 14, 16, 18] [3, 6, 9, 12, 15, 18, 21, 24, 27] [4, 8, 12, 16, 20, 24, 28, 32, 36] [5, 10, 15, 20, 25, 30, 35, 40, 45] [6, 12, 18, 24, 30, 36, 42, 48, 54] [7, 14, 21, 28, 35, 42, 49, 56, 63] [8, 16, 24, 32, 40, 48, 56, 64, 72] [9, 18, 27, 36, 45, 54, 63, 72, 81]
- public class Janken12 {
- public static void main(String[] args) {
- String[] te = {"ぐー", "ちょき", "ぱー"};
- char[][] kekka = {{'分', '勝', '負'} // ぐー
- , {'負', '分', '勝'} // ちょき
- , {'勝', '負', '分'}}; // ぱー
- //コンピューター ぐー ちょき ぱー
- int win = 0;
- int lose = 0;
- int draw = 0;
- for (int i = 0; i < args.length; i++) {
- // ここにコードを記述してください。
- }
- System.out.println(win + " 勝 " + lose + " 負 " + draw + " 分 ");
- }
- }
□ 実行結果
$ java Janken12 0 1 2 0 1 2 あなた: ぐー, コンピューター: ぐー --> 結果: 分 あなた: ちょき, コンピューター: ぱー --> 結果: 勝 あなた: ぱー, コンピューター: ぱー --> 結果: 分 あなた: ぐー, コンピューター: ちょき --> 結果: 勝 あなた: ちょき, コンピューター: ぐー --> 結果: 負 あなた: ぱー, コンピューター: ぐー --> 結果: 勝 3 勝 1 負 2 分
- public class Match03 {
- public static void main(String[] args) {
- int[] a = {(int)(Math.random() * 10), (int)(Math.random() * 10), (int)(Math.random() * 10)};
- int[] n = {Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2])};
- int s = 0;
- System.out.println("コンピューターの数字 = " + ArrayUtil.toString(ArrayUtil.sort(a)));
- System.out.println(" あなたの数字 = " + ArrayUtil.toString(ArrayUtil.sort(n)));
- // ここにコードを記述してください。
- switch (s) {
- case 0:
- System.out.println("まったく違います。");
- break;
- case 1:
- System.out.println("やっと合いましたね。");
- break;
- case 2:
- System.out.println("もう少しです。");
- break;
- default:
- System.out.println("おめでとうございます。");
- break;
- }
- }
- }
□ 実行結果
$ java Match03 2 4 4 コンピューターの数字 = [4, 5, 7] あなたの数字 = [2, 4, 4] やっと合いましたね。 $ java Match03 2 4 4 コンピューターの数字 = [1, 1, 7] あなたの数字 = [2, 4, 4] まったく違います。
- public class Igo01 {
- public static void main(String[] args) {
- int n = 19;
- char[][] ban = new char[n][n];
- char[] ishi = {'\u3000'/*日本語の空白文字*/, '●', '○'};
- // コードを記述してください。
- ArrayUtil.printArray(ban);
- }
- }
- // 0712
- public class ArrayUtil {
- :
- static void printArray(char[][] a) {
- // コードを記述してください。
- }
- }
□ 実行結果
$java Igo01 ● ◯ ● ◯ ● ● ◯ ● ● ● ● ● ◯ ● ◯ ◯ ● ◯ ● ◯ ● ● ● ◯ ● ● ◯ ● ● ◯ ◯ ◯ ◯ ◯ ◯ ◯ ● ◯ ◯ ◯ ● ◯ ● ◯ ◯ ◯ ◯ ◯ ◯ ● ◯ ● ◯ ◯ ● ● ◯ ● ◯ ◯ ● ● ● ● ◯ ● ◯ ◯ ● ● ● ◯ ◯ ◯ ● ◯ ● ◯ ● ● ◯ ◯ ● ◯ ◯ ● ● ◯ ◯ ◯ ◯ ◯ ● ● ● ◯ ● ◯ ● ● ● ◯ ● ● ● ● ◯ ◯ ● ◯ ● ◯ ◯ ● ◯ ◯ ◯ ◯ ● ◯ ◯ ◯ ● ● ● ● ◯ ● ● ◯ ◯ ● ◯ ◯ ● ● ● ● ◯ ◯ ◯ ● ● ● ◯ ● ◯ ● ◯ ● ◯ ● ◯ ◯ ● ◯ ◯ ◯ ◯ ● ● ◯ ● ◯ ● ● ◯ ◯ ● ● ● ● ● ● ● ◯ ◯ ● ● ◯ ◯ ◯ ● ◯ ● ◯ ● ◯ ◯ ◯ ◯ ◯ ◯ ◯ ● ● ◯ ● ● ● ● ◯ ● ● ◯ ◯ ◯ ● ◯ ◯ ● ● ● ◯ ◯ ● ● ◯ ● ● ● ◯ ● ◯ ◯ ● ● ● ● ◯ ● ●
- public class Encoding03 {
- public static void main(String[] args) {
- char[] a = args[0].toCharArray();
- int key = 0x3;
- // ここにコードを記述してください。
- System.out.println("'" + args[0] + "' encode to: '" + e + "'");
- }
- }
□ 実行結果
$ java Encoding03 "Hello World" 'Hello World' encode to: 'Kfool#Tlqog'
- public class ReversePolishNotation {
- int[] stack = new int[2];
- int sp;
- int calc(String str) {
- int d1, d2;
- String s;
- sp = 0;
- for (int i = 0; i < str.length(); i++) {
- switch (str.charAt(i)) {
- case '+':
- d1 = pop();
- d2 = pop();
- push(d2 + d1);
- break;
- // ここにコードを記述してください。
- default:
- push(Integer.parseInt(str.substring(i, i+1)));
- }
- }
- return pop();
- }
- void push(int d) {
- stack[sp++] = d;
- }
- int pop() {
- return stack[--sp];
- }
- public static void main(String[] args) {
- System.out.println("計算式 = " + args[0]);
- ReversePolishNotation rpn = new ReversePolishNotation();
- System.out.println("結果 = " + rpn.calc(args[0]));
- }
- }
□ 実行結果
$ java ReversePolishNotation 98+7%6*5/4- 計算式 = 98+7%6*5/4- <-- この式は、((((9+8)%7)*6)/5)-4 を意味します。 結果 = -1
- public class Eratosthenes {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]);
- boolean[] prime = new boolean[n];
- for (int i = 0; i < n; i++) {
- prime[i] = true;
- }
- // ここにコードを記述してください。
- for (int i = 2; i < n; i++) {
- if (prime[i]) {
- System.out.print(" " + i);
- }
- }
- System.out.println();
- }
- }
□ 実行結果
$ java Eratosthenes 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
- public class CheckDigit01 {
- final static int[] weight = {19, 17, 13, 11, 7, 1};
- final static int n = weight.length;
- public static void main(String[] args) {
- int[] digit = new int[n];
- int len = args[0].length();
- if (len != n - 1 && len != n) {
- System.out.println("5桁または6桁の数字を指定してください。");
- System.exit(1);
- }
- for (int i = 0; i < Math.min(n, len); i++) {
- digit[i] = Integer.parseInt(args[0].substring(i, i+1));
- }
- if (len == n - 1) {
- System.out.println("チェック・ディジットつきの数は "
+ setCheckDigit(digit) + " です。");- } else if (isValidDigit(digit)) {
- System.out.println(args[0] + "は正しい数です。");
- } else {
- System.out.println(args[0] + "は正しくありません。");
- }
- }
- static String setCheckDigit(int[] d) {
- // ここにコードを記述してください。
- return sb.toString();
- }
- static boolean isValidDigit(int[] d) {
- // ここにコードを記述してください。
- return((s % 10) == 0);
- }
- }
□ 実行結果
$ java CheckDigit01 01234 チェック・ディジットつきの数は 012346 です。 $ java CheckDigit01 012346 012346は正しい数です。 $ java CheckDigit01 012345 012345は正しくありません。