Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Creazione dei Nostri Metodi | Metodi
Java Avanzato
course content

Contenuti del Corso

Java Avanzato

Java Avanzato

1. Struttura Profonda di Java
2. Metodi
3. String Avanzato
4. Classi
5. Classi Avanzate

book
Creazione dei Nostri Metodi

Come si usano i metodi?

Abbiamo visto come creare un metodo che somma due numeri. Ora vediamo come possiamo effettivamente chiamare questo metodo nel nostro codice:

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; } }
  • Abbiamo creato due variabili di tipo int con valori diversi e poi le abbiamo passate come parametri al nostro metodo;
  • Poiché il nostro metodo restituisce un tipo int, possiamo inizializzare la variabile sum con il risultato della chiamata al metodo;
  • In questo modo, il nostro metodo ha calcolato e restituito la sum dei due numeri.

Possiamo anche passare array come parametri e restituirli dai metodi. Vediamo un esempio:

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

Abbiamo scritto un metodo che ordina e incrementa ciascun elemento di un array di interi (int[]). Successivamente creiamo un array non ordinato e utilizziamo il metodo su di esso, inizializzando un nuovo array chiamato newArray con il valore restituito.

Vale la pena notare che possiamo utilizzare questo metodo più volte nello stesso codice, ad esempio:

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

Pertanto, possiamo osservare che è possibile utilizzare il nostro metodo più volte nel codice. Lo abbiamo applicato a due diversi array di interi, e ciascuno di essi risulta ora ordinato e incrementato.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

course content

Contenuti del Corso

Java Avanzato

Java Avanzato

1. Struttura Profonda di Java
2. Metodi
3. String Avanzato
4. Classi
5. Classi Avanzate

book
Creazione dei Nostri Metodi

Come si usano i metodi?

Abbiamo visto come creare un metodo che somma due numeri. Ora vediamo come possiamo effettivamente chiamare questo metodo nel nostro codice:

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; } }
  • Abbiamo creato due variabili di tipo int con valori diversi e poi le abbiamo passate come parametri al nostro metodo;
  • Poiché il nostro metodo restituisce un tipo int, possiamo inizializzare la variabile sum con il risultato della chiamata al metodo;
  • In questo modo, il nostro metodo ha calcolato e restituito la sum dei due numeri.

Possiamo anche passare array come parametri e restituirli dai metodi. Vediamo un esempio:

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

Abbiamo scritto un metodo che ordina e incrementa ciascun elemento di un array di interi (int[]). Successivamente creiamo un array non ordinato e utilizziamo il metodo su di esso, inizializzando un nuovo array chiamato newArray con il valore restituito.

Vale la pena notare che possiamo utilizzare questo metodo più volte nello stesso codice, ad esempio:

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

Pertanto, possiamo osservare che è possibile utilizzare il nostro metodo più volte nel codice. Lo abbiamo applicato a due diversi array di interi, e ciascuno di essi risulta ora ordinato e incrementato.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt