Why is this void method not working as I would expect?

1.9k views Asked by At

Assuming that the Given array is instantiated as follows: int[] given = {1, 2, 3, 4, 5};

These methods should change the array and show the print results.

public int[] change(int[] given) {
   out.println(Arrays.toString(given)); //before-changes
   whatIsGoingOn(given); //make changes
   out.println(Arrays.toString(given));//after changes
   return given; //return the supposedly changed array
}
//Make changes:
public void whatIsGoingOn(int[] given) {
   given = new int[5];
   given[0] = 200;
}

I BELIVE THE PROBLEM to be with the void method, overriding the original values of the int[] given but the new initialization and declarations never make it out.

I Would expect the array to be:

{200, 0, 0, 0, 0};

But Java prints out:

{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}

But in the case where I returned a newly changed array in the whatIsGoingOnMethod, I get my wanted results:

{1, 2, 3, 4, 5}
{200, 0, 0, 0, 0}

If the void method just does something and its not really being "applied" as I would like to expect. I'd appreciate an illuminating explanation for this.

This Question is ALREADY ANSWERED THANKS!!! I need to freeze it.

5

There are 5 answers

4
D. Ben Knoble On BEST ANSWER

With given = new int[5]; you are changing what the local variable given points to, not the object that it used to point to. The local is later out of scope. The array is never modified.

Youre creating a whole new array, and assigning the variable that used to refer to the original to the new array. Youre modifying the new array, and never touching the old one.

The appropriate fix would be similar to what follows:

public static void main(String[] args)
{
    int[] original = new int[5];//already allocating memory, initializing to default value, etc.
    setFirstTo20(original);
    System.out.println(original[0]);//still 20!
}

public static void setFirstTo20(int[] given)
{
    for (int i : given)
        System.out.println(i);//for debugging, before change
    given[0] = 20;
    for (int i : given)
        System.out.println(i);//after change
    //heres whats happening to you
    given = new int[5];
    given[0] = 40;
    for (int i : given)
        System.out.println(i);//show new changes to NEW array
}

Obviously you should get rid of the extraneous code if you intend for this or similar methods to be used in actual classes.

0
omikes On

Void quite simply will return nothing. Replace void with int[] to achieve your expected results. Also replace

public void whatIsGoingOn(int[] given) {
   given = new int[5];
   given[0] = 200;
}

with

public int[] whatIsGoingOn(int[] given) {
   given = new int[5];
   given[0] = 200;
   return given;
}

the final result should be

public int[] change(int[] given) {
   out.println(Arrays.toString(given)); //before-changes
   given = whatIsGoingOn(given); //make changes
   out.println(Arrays.toString(given));//after changes
   return given; //return the supposedly changed array
}
//Make changes:
public int[] whatIsGoingOn(int[] given) {
   given = new int[5];
   given[0] = 200;
   return given;
}
0
ILMTitan On

Arrays such as int[] are reference types. This means that when you do the assignment given = new int[5], you are chainging the array referenced within whatIsGoingOn() and not affecting the array referenced within change().

To do what you want it to, you need to loop though the array and change the individual elements within the array.

for(int i = 0; i < given.length; i++) {
    given[i] = 0;
}
given[0] = 200;
0
Donghua Li On

Because the argument given of function whatIsGoingOn is passed by value. To change it in a void function, you have to wrap it in an object like this:

class ArrayRef {
    public int[] array;
}
public int[] change(int[] given) {
    out.println(Arrays.toString(given)); //before-changes
    ArrayRef ref = new ArrayRef();
    ref.array = given; //set a reference to the given array
    whatIsGoingOn(ref); //make changes
    given = ref.array; //get the changed array
    out.println(Arrays.toString(given));//after changes
    return given; //return the supposedly changed array
}
//Make changes:
public void whatIsGoingOn(ArrayRef ref) {
    ref.array = new int[5];
    ref.array[0] = 200;
}
0
Sarit Adhikari On

change following lines of whatIsGoingOn method from

given = new int[5];
given[0] = 200;

to

Arrays.fill(given,0);
given[0] = 200;

It still holds reference to original array .No need to return anything.