简体   繁体   中英

How to create a method with 2 arrays as parameters?

How can I pass 2 predefined arrays as parameters to a method, here is my tried code:

   public class MergeArrays {
    public int array1[] = new int[5];
    public int array2[] = new int[5];
    private int[] mergeArray(array1,array2){

    }
}

To answer your specific question "How can I pass 2 predefined arrays as parameters to a method", you need to specify the types of the parameters in your method:

private int[] mergeArray(int[] array1, int[] array2)

Then you can call it with:

mergeArray(array1, array2);

A bigger example:

public class MergeArrays {

    public int array1[] = new int[5];
    public int array2[] = new int[5];

    private int[] mergeArray(int[] array1, int[] array2) {
        // TODO
    }


    public static void main(String... args) {

        MergeArrays merger = new MergeArrays();
        merger.array1[0] = 18;
        merger.array1[1] = 28;
        merger.array2[0] = 991;
        // etc.

        int[] mergedArray = merger.mergeArray(array1, array2);
        // TODO
    }
}

It's worth mentioning this is not great code.

The names of the parameters in the method clash with (and conceal) the names of the instance variables. This may be confusing and if you want to access the instance variables from within the method you'll have to prefix them with this.

In general in object-oriented programs, you want to hide internal data representations like the arrays, rather than making them public. For example, perhaps later you will want to use an ArrayList instead. If you make the fields public then other classes may refer to them and so changes will cause knock-on effects in the code base.

Your mergeArray method would be better named merge or mergeArrays (plural) and should be static unless it depends on the internal state of the class.

Answer:

  • you can create a method which takes two array as parameters like :

Syntax :

 modifier return-type nameOfMethod (type_of_array
    array_name[],type_of_array array_name[]) 
         {
            // method body
         }

Example:

  public void TakeArray(int[] a, int[] b) {

   }
  • How to pass array as argument to this method:

Syntax:

 nameOfMethod (name_of_Predefined_array);

Example:

int array1[] = { 1, 2, 1, 1, 1, 1, 2 };  
int array2[] = { 1, 2, 1, 1, 1, 1, 2 }; 

TakeArray(array1, array2); 

Working code example :

public class Consecutive {

    public static void TakeArray(int[] a, int[] b) { // define a function to
                                                        // take 2 array as input
        System.out.println("Array 1 :");
        for (int i : a)
            // printing value of array 1
            System.out.println(i);

        System.out.println("Array 2 :");

        for (int i : b)
            // printing value of array 1
            System.out.println(i);

    }

    public static void main(String[] args) {
        int array1[] = { 1, 2, 1, 1, 1, 1, 2 }; // creating an array
        int array2[] = { 1, 2, 1, 1, 1, 1, 2 };// creating another array
        TakeArray(array1, array2); // passing both array to function as
                                    // arguments
    }

}

Check the explanation in comments within code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM