1

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.

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.

Corey Bryant
  • 33
  • 1
  • 2
  • 13

5 Answers5

2
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.

Cinnam
  • 1,892
  • 1
  • 15
  • 23
Kiran Indukuri
  • 402
  • 3
  • 9
2

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 myArray and you can pass it into the method like this:

doubleArray (myArray);

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

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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

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 need to return the array. You're modifying the original array anyway, so any changes you make will persist outside of this method's scope.

Ben M.
  • 2,370
  • 1
  • 15
  • 23
0

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.

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.

Sandeep Poonia
  • 2,158
  • 3
  • 16
  • 29
0

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"?

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.

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;
    }
}
Community
  • 1
  • 1
Eugene
  • 10,627
  • 5
  • 49
  • 67