Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Création de Nos Propres Méthodes | Méthodes
Java Avancé
course content

Contenu du cours

Java Avancé

Java Avancé

1. Deep Java Structure
2. Méthodes
3. String Avancé
4. Classes
5. Classes Avancées

book
Création de Nos Propres Méthodes

Comment utiliser les méthodes ?

Nous avons examiné la création d'une méthode qui additionne deux nombres. Voyons maintenant comment nous pouvons réellement appeler cette méthode dans notre code :

Main.java

Main.java

copy
123456789101112131415161718192021
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; } }
  • Nous avons créé deux variables de type int avec des valeurs différentes, puis nous les avons passées en tant que paramètres à notre méthode ;
  • Puisque notre méthode retourne un type int, nous pouvons initialiser la variable sum avec le résultat de l'appel à la méthode ;
  • Ainsi, notre méthode a calculé et retourné la sum des deux nombres.

Nous pouvons également passer des tableaux en tant que paramètres et les retourner depuis des méthodes. Voyons un exemple :

Main.java

Main.java

copy
1234567891011121314151617181920212223242526272829303132
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; } }

Nous avons écrit une méthode qui trie et incrémente chaque élément d'un tableau d'entiers (int[]). Ensuite, nous créons un tableau non trié et utilisons la méthode sur celui-ci, en initialisant un nouveau tableau appelé newArray avec la valeur retournée.

Il convient de noter que nous pouvons utiliser cette méthode plusieurs fois dans le même code, par exemple :

Main.java

Main.java

copy
1234567891011121314151617181920212223242526272829303132333435363738
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; } }

Ainsi, nous pouvons constater que notre méthode peut être utilisée plusieurs fois dans le code. Nous l'avons appliquée à deux tableaux d'entiers différents, et chacun d'eux est désormais trié et incrémenté.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 2

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

course content

Contenu du cours

Java Avancé

Java Avancé

1. Deep Java Structure
2. Méthodes
3. String Avancé
4. Classes
5. Classes Avancées

book
Création de Nos Propres Méthodes

Comment utiliser les méthodes ?

Nous avons examiné la création d'une méthode qui additionne deux nombres. Voyons maintenant comment nous pouvons réellement appeler cette méthode dans notre code :

Main.java

Main.java

copy
123456789101112131415161718192021
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; } }
  • Nous avons créé deux variables de type int avec des valeurs différentes, puis nous les avons passées en tant que paramètres à notre méthode ;
  • Puisque notre méthode retourne un type int, nous pouvons initialiser la variable sum avec le résultat de l'appel à la méthode ;
  • Ainsi, notre méthode a calculé et retourné la sum des deux nombres.

Nous pouvons également passer des tableaux en tant que paramètres et les retourner depuis des méthodes. Voyons un exemple :

Main.java

Main.java

copy
1234567891011121314151617181920212223242526272829303132
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; } }

Nous avons écrit une méthode qui trie et incrémente chaque élément d'un tableau d'entiers (int[]). Ensuite, nous créons un tableau non trié et utilisons la méthode sur celui-ci, en initialisant un nouveau tableau appelé newArray avec la valeur retournée.

Il convient de noter que nous pouvons utiliser cette méthode plusieurs fois dans le même code, par exemple :

Main.java

Main.java

copy
1234567891011121314151617181920212223242526272829303132333435363738
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; } }

Ainsi, nous pouvons constater que notre méthode peut être utilisée plusieurs fois dans le code. Nous l'avons appliquée à deux tableaux d'entiers différents, et chacun d'eux est désormais trié et incrémenté.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 2
some-alt