如何在java中调用带有数组参数的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20314305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:01:05  来源:igfitidea点击:

How do I invoke a method with array parameter in java?

javaarraysmethodsinvocation

提问by user3054791

I have an assignment in which I have to perform operations on array in Java, I have to make separate functions of each operation, which I will write but I can not figure out how to invoke a method with array parametres. I usually program in c++ but this assignment is in java. If any of you can help me, I'd be really thankful. :)

我有一个任务,我必须在 Java 中对数组执行操作,我必须为每个操作创建单独的函数,我将编写这些函数,但我无法弄清楚如何使用数组参数调用方法。我通常用 c++ 编程,但这个作业是用 java 编写的。如果你们中有人能帮助我,我将不胜感激。:)

public class HelloJava {
    static void inpoot() {
        Scanner input = new Scanner(System.in);
        int[] numbers = new int[10];

        System.out.println("Please enter 10 numbers ");
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextInt();
        }
    }

    static void outpoot(int[] numbers) {
        for(int i = 0; i < numbers.length; i++) { 
                System.out.println(numbers[i]); 
        }
    }

    public static void main(String[] args) {
        inpoot();
        outpoot(numbers); //can not find the symbol
    }
}

采纳答案by Julián Urbano

Your inpootmethod has to return the int[]array, and then you pass it to outpootas a parameter:

您的inpoot方法必须返回int[]数组,然后将其outpoot作为参数传递给:

public class HelloJava {    
    static int[] inpoot() { // this method has to return int[]
        Scanner input = new Scanner(System.in);
        int[] numbers = new int[10];

        System.out.println("Please enter 10 numbers ");
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextInt();
        }
        return numbers; // return array here
    }

    static void outpoot(int[] numbers) {
        for(int i = 0; i < numbers.length; i++) { 
            System.out.println(numbers[i]); 
        }
    }

     public static void main(String[] args) {
        int[] numbers = inpoot(); // get the returned array
        outpoot(numbers); // and pass it to outpoot
    }
}

回答by Anthony Porter

When you call outpoot it should be outpoot (numbers);

当您调用 outpoot 时,它应该是 outpoot(数字);