Kursinhalt
Java Erweitert
Java Erweitert
Eigene Methoden Erstellen
Wie verwendet man Methoden?
Wir haben uns angesehen, wie man eine Methode erstellt, die zwei Zahlen addiert. Schauen wir uns nun an, wie wir diese Methode tatsächlich in unserem Code aufrufen können:
Main.java
package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { int a = 10; int b = 31; // use the method to add two numbers: `a` and `b` int sum = addTwoNumbers(a, b); // print the sum of the two numbers System.out.println(sum); } // method to add two numbers and return the result static int addTwoNumbers(int firstNumber, int secondNumber) { int result = firstNumber + secondNumber; // return the result of the addition return result; } }
- Es wurden zwei Variablen vom Typ
int
mit unterschiedlichen Werten erstellt und anschließend als Parameter an unsere Methode übergeben; - Da unsere Methode einen Wert vom Typ
int
zurückgibt, kann die Variablesum
mit dem Ergebnis des Methodenaufrufs initialisiert werden; - Somit hat unsere Methode die
sum
der beiden Zahlen berechnet und zurückgegeben.
Wir können auch Arrays als Parameter übergeben und von Methoden zurückgeben. Schauen wir uns ein Beispiel an:
Main.java
package com.example; import java.util.Arrays; // do not modify the code below this comment public class Main { public static void main(String[] args) { // creating an unsorted array int[] array = {4, -1, 5, 0, 4, -6, 2, 7, 4, 1}; // use a method to sort the array and increment each element by 1 int[] newArray = sortAndIncrementEveryElement(array); // print the new array after sorting and incrementing each element for (int element : newArray) { System.out.print(element + " "); // output each element from the new array } } // method to increment each element of the array by 1 and then sort it static int[] sortAndIncrementEveryElement(int[] inputArray) { // create a result array with the same length as the input array int[] result = new int[inputArray.length]; // increment each element by 1 for (int i = 0; i < inputArray.length; i++) { result[i] = inputArray[i] + 1; } // sort the result array Arrays.sort(result); // return the sorted and incremented array return result; } }
Wir haben eine Methode geschrieben, die ein Integer-Array (int[]
) sortiert und jedes Element inkrementiert. Anschließend erstellen wir ein unsortiertes Array und wenden die Methode darauf an, indem wir ein neues Array namens newArray
mit dem zurückgegebenen Wert initialisieren.
Es ist erwähnenswert, dass wir diese Methode mehrfach im selben Code verwenden können, zum Beispiel:
Main.java
package com.example; import java.util.Arrays; // do not modify the code below this comment public class Main { public static void main(String[] args) { // creating unsorted arrays int[] array = {4, -1, 5, 0, 4, -6, 2, 7, 4, 1}; int[] secondArray = {2, 2, 0, -5, 1, 8, 13, -9, 0}; // use a method to sort and increment each element in both arrays int[] newArray = sortAndIncrementEveryElement(array); int[] newSecondArray = sortAndIncrementEveryElement(secondArray); // print the new arrays after sorting and incrementing the elements for (int element : newArray) { System.out.print(element + " "); // output each element of the first array } System.out.println(System.lineSeparator()); for (int element : newSecondArray) { System.out.print(element + " "); // output each element of the second array } } // method to increment each element by 1 and then sort the array static int[] sortAndIncrementEveryElement(int[] inputArray) { // create a result array with the same length as the input array int[] result = new int[inputArray.length]; // increment each element by 1 for (int i = 0; i < inputArray.length; i++) { result[i] = inputArray[i] + 1; } // sort the result array Arrays.sort(result); // return the sorted and incremented array return result; } }
Daraus ergibt sich, dass wir unsere Methode mehrfach im Code verwenden können. Wir haben sie auf zwei verschiedene Integer-Arrays angewendet, und jedes von ihnen ist nun sortiert und inkrementiert.
Danke für Ihr Feedback!