Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

pass array to method java

/*Passing Array to Method , tow ways to passing 1: with method name, 2: passing Anonymous Array  
Means -> array can be invoking  by passing an initialized array element to the method.*/
/*  first method */
public class PassArray {
    // creating method and receiving array as parameter
    static void MethodName(double[] array) {
        // foreach loop for printing array elements
        for (double element : array) {
            System.out.println(element);
        }
    }

    public static void main(String[] args) {
        // Declaring and Initializing an array
        double[] A = { 1.1, 2.4, 4.5, 5.9 };
        MethodName(A);// passing array to method
    }
}
/* Second method*/

public class PaasArrToMth {

    static void MethodName(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    public static void main(String[] args) {
        // Anonymous array means without name
        MethodName(new int[] { 3, 1, 2, 6, 4, 2 });// passing anonymous array to method
        // don't need to declare the array while passing an array to the method.
    }

}
 
PREVIOUS NEXT
Tagged: #pass #array #method #java
ADD COMMENT
Topic
Name
4+7 =