Information Technology

How to pass Arrays to Methods in Java?

  • You can start the dialog one by one. If we define dialogue as an argument, the topic of discussion in Article ref. Therefore, any changes in this dialog interrupted the discussion.
  • Since you can change the old key path on the path, you can change the dialog on the path. Hold the title of the discussion without square footage during the conversation to put the discussion on the page. Unlike C / C ++, in Java every dialog knows how long it takes to use its parameters, so we do not want to write one part as an argument as another argument. For example, we have a ten-line number.

Example

  1. import java.util.Scanner;
  2. public class ArraysToMethod {
    public int max(int [] array) {
    int max = 0;
  3. for(int i=0; i<array.length; i++ ) {
    if(array[i]>max) {
    max = array[i];
    }
    }
    return max;
    }
  4. 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;
    }
  5. public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter the size of the array that is to be created::”);
    int size = sc.nextInt();
    int[] myArray = new int[size];
    System.out.println(“Enter the elements of the array ::”);
  6. for(int i=0; i<size; i++) {
    myArray[i] = sc.nextInt();
    }
    ArraysToMethod m = new ArraysToMethod();
    System.out.println(“Maximum value in the array is::”+m.max(myArray));
    System.out.println(“Minimum value in the array is::”+m.min(myArray));
    }
    }

Output:

Enter the size of the array that is to be created ::
6
Enter the elements of the array ::
12
7
43
64
87
Maximum value in the array is ::87
Minimum value in the array is ::7

In this example: we share the numbers with the concept of converting matrices to methods. Informal conversations are converted to method (). In a more accurate translation, the instructions for the use of numerical methods and inversions were given.

Passing Arguments to Methods in Java

There are two ways of passing arguments to methods in Java:

    1. Pass by value
    2. Pass by reference

1. Pass by value

If the dispute is outside the scope of the pricing method, only a short copy of the change will be sent to the receiving method. Internal boundary changes are not caused by the phone. This is to ensure that the change on the phone remains unchanged after a return.

Example:
  1. import <a href="data-internallinksmanager029f6b8e52c="16">java</a>.io.*;
  2. public class Sum {
  3.  static int CaiculateTotal(int n1) {
  4.  int total=0;
  5.  int marks[] = new int[6]:
  6.  try {
  7.    BufferedReader br= newBufferedReader( new InputStreamReader(System.in));
  8.    for(int i=0; i&lt;n 1; i++) {
  9.      marks[i]= Integer.parselnt(br.readLine());
  10.      total+=marks[i];
  11.    }
  12.  }
  13.    catch(Exception e) {
  14.      System.out.println("Array out of range");
  15.    }
  16.      return total;
  17. }
  18.   public static void main(String args[]) throws IOException {
  19.     int n,Max;
  20.     System.out.println("Enter the numbers :");
  21.     Max = CalculateTotal(6);
  22.     System.out.println("Sum of the numbers is:" +Max);
  23.  }
  24. }
output:

Enter the numbers

5
6
7
Sum of the numbers is: 18

The program converts the value 6 to a common value. Calculating Total n1 is a great way to stop 6 at a time.

2. Pass by reference

In turn, before it is on its way, the return rotation returns to the correct path. This feature does not use Java to separate call paths. Instead of planning, strategies are planned based on usage, and therefore each change is selected. The program offers Java phones over one application.

Example
Program to illustrate pass by reference.
  1. import java.io.*;
  2. class swap_ref {
  3.  static void swap(First ob) {
  4.  int temp;
  5.  temp=ob.a;
  6.  ob.a=ob.b;
  7.  ob.b=temp;
  8.  }
  9.  public static void main(String args[ ]) {
  10.   First ob=new First(10,20);
  11.   System.out.println("Before SWAP");
  12.   System.out.println("a=" +ob.a+"b=" +ob.b);
  13.   swap(ob);
  14.   System.out.println("After SWAP");
  15.   System.out.println ("a="+ob.a +"b=" +ob.b);
  16.  }
  17. }
  18.  class First {
  19.   int a;
  20.   int b;
  21.   First(int x, int y) {
  22.     a = x;
  23.     b = y;
  24.   }
  25.  }
output:

Before SWAP

a = 10
b = 20
After SWAP
a = 20

b = 10

Here differences of opinion are called two kinds, and every deception thus affects the old. Therefore, the change in value after the changed estimate is called the zero value.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button