メソッドの練習
メニューを表示するにはスワイプしてください
練習
メソッドは理解が難しい場合があるため、この章ではさまざまなメソッドの例を紹介し、どのように動作するか、どこで使用するか、そして複数のメソッドを効果的に記述・組み合わせる方法を示します。
例 1
まず、型がintの配列を受け取り、最後の値が最初になり、最初の値が最後になるように配列を反転するメソッドから始めましょう。
Main.java
123456789101112131415161718192021222324252627282930package com.example; public class Main { static int[] flipIntArray(int[] array) { int[] result = new int[array.length]; int index = 0; for (int i = array.length - 1; i >= 0; i--) { result[index] = array[i]; index++; } return result; } static void printIntArrayToTheConsole(int[] array) { for (int element : array) { System.out.print(element + " "); } System.out.println(); } public static void main(String[] args) { // do not modify the code below int[] firstArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int[] secondArray = {0, 2, 4, 6, 8, 10, 12, 14, 16}; printIntArrayToTheConsole(flipIntArray(firstArray)); printIntArrayToTheConsole(flipIntArray(secondArray)); } // do not modify the code above }
-
まず、4行目のメソッド
flipIntArrayに注目してください。このメソッドは配列の末尾からカウントするループを使用し、同じサイズの新しい配列に値を格納します。また、6行目のindex変数は、result配列に値を格納するために増加します。 -
次に、前の章の14行目にあるメソッドを見ることができます。これは配列を1行のコードで出力するために使用しています。
-
さて、興味深い点に進みましょう。25-26行目では、メソッドをパラメータとして別のメソッド内に渡していることがわかります。これは、
int[]を返すメソッドを、int[]をパラメータとして受け取るメソッド内で使用しているためで、変数を作成するために割り当てられるメモリを節約できます。これにより、メソッドが正しく動作し、正しい値が表示されていることが確認できます。
例 2
整数変数を入力として受け取り、階乗を計算するメソッドを記述する必要があるとします。
Main.java
12345678910111213141516171819202122232425262728293031package com.example; // do not modify the code below this comment public class Main { // method to calculate the factorial of a number static int calculateFactorial(int input) { int result = 1; // loop to calculate factorial by multiplying each number from 1 to input for (int i = 1; i <= input; i++) { result = result * i; } // return the final result of the factorial calculation return result; } public static void main(String[] args) { // initializing the first number for factorial calculation int first = 5; // initializing the second number for factorial calculation int second = 7; // initializing the third number for factorial calculation int third = 4; // calculating and printing the factorial of the first number System.out.println("Factorial of the first number = " + calculateFactorial(first)); // calculating and printing the factorial of the second number System.out.println("Factorial of the second number = " + calculateFactorial(second)); // calculating and printing the factorial of the third number System.out.println("Factorial of the third number = " + calculateFactorial(third)); } }
この例では、累積変数を与えられた数まで 1 から順に掛け算することで、階乗 を計算するメソッドを作成しています。シンプルな方法であり、このメソッドを main メソッド内で複数回使用することで、コードの省スペース化と可読性の向上 を実現しています。
次に、メソッドが System.out.println 型の値を返すため、int 内でこのメソッドを使用しています。
例 3
文字列の配列から略語を作成するメソッドを作成します。
Main.java
1234567891011121314151617181920212223242526272829package com.example; // do not modify the code below this comment public class Main { // method to create abbreviation by taking first letter of each word static String makeAbbreviation(String[] input) { StringBuilder result = new StringBuilder(); // iterate through each word and append the first letter to result for (String word : input) { result.append(word.toUpperCase().charAt(0)); } // return the abbreviation as a string return result.toString(); } public static void main(String[] args) { // initializing arrays with words for abbreviation String[] firstArray = {"united", "nations"}; String[] secondArray = {"North", "Atlantic", "Treaty", "Organization"}; String[] thirdArray = {"Oh", "my", "God"}; // printing abbreviation for the first array System.out.println(makeAbbreviation(firstArray)); // printing abbreviation for the second array System.out.println(makeAbbreviation(secondArray)); // printing abbreviation for the third array System.out.println(makeAbbreviation(thirdArray)); } }
このメソッドでは、StringBuilder クラスを使用して append() メソッドを呼び出し、最初は空の文字列に大文字の最初の文字を追加します。この方法で、配列の各要素を順に処理し、ゼロ番目の位置(各単語の最初の文字)の文字を取得し、toUpperCase() メソッドで大文字に変換し、append() メソッドで結果に追加します。最後に、toString() メソッドを使って StringBuilder を String に変換します。
1. メソッドにおける戻り値とは何ですか?
2. void メソッドは何を返しますか?
3. メソッドは他のメソッドの中で使用できますか?
フィードバックありがとうございます!
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください