logo

Sender Array til at fungere i Java

Funktioner bruges til at bryde og opdele en kæmpe kode i små bidder, så koden bliver mere forståelig og dermed reducerer kodens kompleksitet. Arrays er de homogene datastrukturer til at reducere kodens kompleksitet, øge effektiviteten og reducere kodens eksekveringstid. Vi gør brug af arrayet og tænker på, at begge disse tidsbesparende dele af programmering implementeres sammen. Derved eksisterer begrebet bestået Array at fungere.

I dette afsnit vil vi lære, hvordan vi kan videregive et array til en brugerdefineret funktion og vil bemærke, hvordan det gør koden mere optimistisk såvel som effektiv.

Generelt er formålet med at overføre et array til en funktion at overføre en stor mængde data mellem metoder. For at videregive et array til en funktion, skal du blot videregive arrayet som funktions parameter (som normale variabler), og når vi sender et array til en funktion som et argument, overføres faktisk adressen på arrayet i hukommelsen, hvilket er reference. Således vil enhver ændring i arrayet inden for metoden påvirke de faktiske arrayværdier.

Vi vil implementere nogle eksempelkoder, hvorigennem vi vil lære, hvordan man sender et array til en funktion.

Eksempel 1:

Beregning af maksimum- og minimumværdierne for en given matrix.

For at få maksimum- og minimumværdien i et array kan vi udføre nedenstående kode:

 import java.util.Scanner; public class array { public int max(int [] array) { int max = 0; for(int i=0; imax) { max = array[i]; } } return max; } public int min(int [] array) { int min = array[0]; for(int i = 0; i <array.length; i++ ) { if(array[i]<min) min="array[i];" } return min; public static void main(string args[]) scanner sc="new" scanner(system.in); system.out.println('enter the array range'); int size="sc.nextInt();" int[] arr="new" int[size]; elements of ::'); for(int i="0;" i<size; i++) arr[i]="sc.nextInt();" m="new" array(); system.out.println('maximum value in is::'+m.max(arr)); system.out.println('minimum is::'+m.min(arr)); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/73/passing-array-function-java.webp" alt="Passing Array to Function In Java"> <p> <strong>Code Explanation:</strong> </p> <ul> <li>In the above code, we created a class <strong>array</strong> within which we have created a <strong>max()</strong> function and <strong>min()</strong> </li> <li>In the <strong>max ()</strong> function, we have passed an array as the function parameter, and using for loop, we found the maximum value in the array by indexing i from 0 to the length of the array, and as soon it finds the maximum value, it gets stored in the max variable. The condition is that if any of the elements present in the array is greater than the max variable (i.e., max=0 initially), then only it will assign that maximum value to the array.</li> <li>Next, in the min () function, we performed the same, but in this, we have found the minimum value present in the array.</li> <li>Finally, in the main () method, we printed the obtained max and min values from the array.</li> </ul> <h3>Example 2:</h3> <p> <strong>Computing an array of Random Numbers</strong> </p> <p>Random numbers are those numbers whose occurrence is random and cannot be predicted reasonably.</p> <p>Below is the example code through which we can understand the passing of an array to a function and generate random numbers:</p> <pre> public class array { public static void main(String[] args) { final int n = 6; int[] random_array; random_array = create_random(n); System.out.println(&apos;The array of random numbers is:&apos;); for (int i = 0; i <random_array.length; i++) { system.out.print(random_array[i] + ' '); } public static int[] create_random(int n) random_array="new" int[n]; for (int i="0;" <random_array.length; random_array[i]="(int)" (math.random() * 10); return random_array; < pre> <p> <strong>On executing the code, we got the below-shown output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/73/passing-array-function-java-2.webp" alt="Passing Array to Function In Java"> <h4>Note: In the case of random numbers, always the result will vary as the numbers are generated randomly.</h4> <h3>Example 3:</h3> <p> <strong>Sorting numbers of an array</strong> </p> <p>Below is an example code where we pass an array to a function and sort the elements of the given array:</p> <pre> class array { public static void main(String[] args) { int[] n={12,24,2,89,34,45}; System.out.println(&apos;Before sorting&apos;); display(n); sort(n); System.out.println(&apos;
 After Sorting :&apos;); display(n); } static void display(int n[]) { for(int i=0; i<n.length;i++) system.out.print(n[i] + ' '); } static void sort(int n[]) { int i, j, temp; for(i="0;" i<n.length-i;i++) for(j="0;" jn[j+1]) temp="n[j];" n[j]="n[j+1];" n[j+1]="temp;" < pre> <p> <strong>When we executed the above code, we got the following result:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/73/passing-array-function-java-3.webp" alt="Passing Array to Function In Java"> <p>These are some of the possible example implementations through which it is understandable that how one can pass an array to a function in Java.</p> <hr></n.length;i++)></pre></random_array.length;></pre></array.length;>