简体   繁体   English

如何创建一个以 2 个数组作为参数的方法?

[英]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:如何将 2 个预定义数组作为参数传递给方法,这是我尝试过的代码:

   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: 要回答您的特定问题“如何将2个预定义数组作为参数传递给方法”,您需要在方法中指定参数的类型:

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. 这可能会造成混淆,如果您要从方法中访问实例变量,则必须为此添加前缀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. 例如,也许以后您将要改用ArrayList 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. 您的mergeArray方法最好命名为mergemergeArrays (复数),并且应该是static除非它依赖于类的内部状态。

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. 检查代码中注释中的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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