logo

Diziyi Java'da İşleve Aktarmak

İşlevler, kodun daha anlaşılır olmasını sağlamak ve böylece kodun karmaşıklığını azaltmak için büyük bir kodu parçalayıp küçük parçalara bölmek için kullanılır. Diziler, kod karmaşıklığını azaltmaya, verimliliği artırmaya ve kodun yürütme süresini azaltmaya yönelik homojen veri yapılarıdır. Diziyi kullanıyoruz ve programlamanın bu zaman kazandıran kısımlarının birlikte uygulandığını düşünüyoruz. Böylece Geçme kavramı ortaya çıkar Sıralamak İşleve.

Bu bölümde, bir diziyi kullanıcı tanımlı bir fonksiyona nasıl geçirebileceğimizi öğreneceğiz ve bunun kodu nasıl daha iyimser ve verimli hale getirdiğini göreceğiz.

Genel olarak bir diziyi bir fonksiyona aktarmanın amacı, yöntemler arasında büyük miktarda veri aktarmaktır. Bir diziyi bir fonksiyona iletmek için, diziyi fonksiyonun parametresi olarak (normal değişkenler olarak) iletmeniz yeterlidir ve bir diziyi bir fonksiyona argüman olarak ilettiğimizde, gerçekte dizinin hafızadaki adresi iletilir, bu da referans. Bu nedenle, yöntem içindeki dizide yapılacak herhangi bir değişiklik, gerçek dizi değerlerini etkileyecektir.

Bir diziyi bir fonksiyona nasıl aktaracağımızı öğreneceğimiz bazı örnek kodları uygulayacağız.

Örnek 1:

Belirli bir dizinin maksimum ve minimum değerlerinin hesaplanması.

Bir dizideki maksimum ve minimum değeri elde etmek için aşağıdaki kodu uygulayabiliriz:

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