java 将数组传递给方法并返回数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32534670/
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-11-02 20:20:35  来源:igfitidea点击:

Pass array to method and return the array

javaarraysmethods

提问by Corey Bryant

Here is the question I am working on

这是我正在研究的问题

Use a for loop to generate an array of ten random integers, all in the range from 100 to 200, inclusive. Use the Arrays class to both sort and display the entire array. Next, pass the array as the sole argument to a method that doubles each element of the array and then returns the array.Use a foreach loop to show all elements in the returned array on one line separated by a single space. This latter loop should also determine the sum of all elements in the returned array. Display the sum with a "thousands" comma as in the sample output.

使用 for 循环生成包含 10 个随机整数的数组,所有整数都在 100 到 200 的范围内,包括 100 到 200。使用 Arrays 类对整个数组进行排序和显示。接下来,将数组作为唯一参数传递给一个方法,该方法将数组的每个元素加倍,然后返回该数组。使用 foreach 循环将返回数组中的所有元素显示在由单个空格分隔的一行上。后一个循环还应确定返回数组中所有元素的总和。以“千”逗号显示总和,如示例输出中所示。

And here is my code:

这是我的代码:

package bryant7and8;

import java.util.Arrays;


public static void main(String[] args) {

    int[] myList = new int[10];

        for (int i = 0; i < myList.length; i++) {
            myList[i] = 100 + (int) (Math.random() * ((110 - 10) + 1));

    }
    Arrays.sort(myList);
    System.out.println(Arrays.toString(myList));

    doubleArray (myList);
    System.out.println(doubleArray(myList));
}

public static int[] doubleArray(int[] array) {
    for (int i = 0; i < array.length; i++) {
        array[i] *= 2;
    }

    return array;
}

}

}

I cannot figure out how to pass the array, I have been at it for a few hours and my books explanation is not helping. Also I apologize for such horrible question formatting I'm still new to stackoverflow.

我不知道如何传递数组,我已经研究了几个小时,我的书籍解释没有帮助。另外,我为这种可怕的问题格式表示歉意,我还是 stackoverflow 的新手。

回答by Kiran Indukuri

void doubleArray(int[] values){
    //change the value array, with doubling logic

}

Since arrays are passed by reference, the passed array will be changed. There is no need to return it.

由于数组是通过引用传递的,因此传递的数组将被更改。没有必要退货。

回答by Sweeper

You should write a method like this:

你应该写一个这样的方法:

public static int[] doubleArray (int[] array) {
    //double the array... I think you know how to do this part.
    return array;
}

Now lets say you have an array called myArrayand you can pass it into the method like this:

现在假设您调用了一个数组myArray,您可以像这样将其传递给方法:

doubleArray (myArray);

And then the elements in myArraywill be doubled. You can also use the return value of the method for some other uses.

然后里面的元素myArray会翻倍。您还可以将该方法的返回值用于其他一些用途。

Alternatively, you can return another brand new array:

或者,您可以返回另一个全新的数组:

public static int[] doubleArray (int[] array) {
    int[] newArray = new int[array.length];
    //put all the stuff in "array" in "newArray"
    //double the new array... I think you know how to do this part.
    return newArray;
}

Then when you called the method, the argument passed in will not be changed. But the return value has the doubled array.

那么当你调用该方法时,传入的参数不会改变。但是返回值具有双倍数组。

回答by Sandeep Poonia

Just simply create a static method(as you will be calling it from static main() method), pass your array as an argument to it. Make some modifications to it.

只需简单地创建一个静态方法(因为您将从静态 main() 方法调用它),将您的数组作为参数传递给它。对其进行一些修改。

public static void doubleTheArrayElements(myList){
//put your logic here
}

In your main method call it: doubleTheArrayElements(myList)No need to return the array as in Java arguments are passed by reference.

在您的主要方法中调用它:doubleTheArrayElements(myList)无需返回数组,因为在 Java 参数中是通过引用传递的。

回答by Eugene

You'd better have faith that java is passed by value. For more details ,refer to Is Java "pass-by-reference" or "pass-by-value"?

你最好相信 java 是通过 value 传递的。有关更多详细信息,请参阅Java 是“按引用传递”还是“按值传递”?

The entire code is like below, and just for your information. After calling public static int[] notByReference(int[] arr)method, myList doesn't point to the new object. Instead, the copied variable has been assigned a new reference to the new object.

整个代码如下所示,仅供参考。调用public static int[] notByReference(int[] arr)方法后,myList 没有指向新对象。相反,复制的变量已分配给新对象的新引用。

import java.util.Arrays;

public class ArrayTenRandom {

    public static void main(String[] args) {

        int[] myList = new int[10];

        for (int i = 0; i < myList.length; i++) {
            myList[i] = 100 + (int) (Math.random() * ((110 - 10) + 1));

        }
        Arrays.sort(myList);
        System.out.println(Arrays.toString(myList));
        doubleArray(myList);
        System.out.println(Arrays.toString(myList));
        notByReference(myList);
        System.out.println(Arrays.toString(myList));

    }

    public static void doubleArray(int[] arr){
        for(int i = 0; i < arr.length; i++){
            arr[i] = arr[i] * 2;
        }
    }

    public static int[] notByReference(int[] arr){
        arr = new int[2];
        System.out.println(Arrays.toString(arr));
        return arr;
    }
}

回答by Ben M.

You just need a method that takes an array as an argument, modifies that array, then returns the same array.

您只需要一个将数组作为参数的方法,修改该数组,然后返回相同的数组。

private int[] doubleArray(int[] arr) {

    for (int i = 0; i < arr.length; i++) {
        arr[i] *= 2;
    }

    return arr;
}

EDIT: As pointed out by other answers, you don't needto return the array. You're modifying the original array anyway, so any changes you make will persist outside of this method's scope.

编辑:正如其他答案所指出的,您不需要返回数组。无论如何,您正在修改原始数组,因此您所做的任何更改都将保留在此方法的范围之外。