1

I have just created a method that is supposed to swap places between index[0] and index[1] in an array.

However I can't seem to find how i should return this to the main method and then print it out in the swapped order.

What did I do wrong?

public class Uppgift4_6a {
    public static void main(String[] args) {
        int [] intArray= new int [] {2, 4};
        int l= intArray[0];
        int r= intArray[1];
        System.out.println("right order:" + l + " " + r);
        exchange(intArray, intArray[0], intArray[1]);
        System.out.println("right order:" + l + " " + r);
    }

    public static void exchange (int[] intArray, int l, int r){
        l = intArray[0]; 
        r = intArray[1];
        intArray[1] = l;
        intArray[0] = r;
        return;
    }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
mackanmorre
  • 145
  • 1
  • 13
  • Passing primitive values (int in this case) to a method **copies** the value. So changing the value in the method does not change the value in the calling method. – Andrew S May 22 '17 at 19:09

4 Answers4

2

You can use :

public static int[] exchange(int[] intArray, int l, int r) {
//             ^^---------------return type
    ....
    return intArray;//<<---------return your result array 
}

To print your array you can use Arrays.toString :

System.out.println(Arrays.toString(exchange(intArray, intArray[0], intArray[1])));;

or you can assign the result to another array for example :

int[] result = exchange(intArray, intArray[0], intArray[1]);
System.out.println(Arrays.toString(result));

Outputs

right order:2 4
[4, 2]
right order:2 4
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

Why do you want to return something ? The array will be updated, as you're passing a reference to an object. However, l and r will not be updated.

Just print out like this

System.out.println("right order:" + l + " " + r);
exchange(intArray, intArray[0], intArray[1]);
System.out.println("right order:" + intArray[0] + " " + intArray[1]);
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
2

Your problem is a misconception:

 exchange(intArray, intArray[0], intArray[1]);

does not at all affect your variables l and r! It updates intArray; but not the primitive values that intArray[0] resp. 1 evaluate to!

Meaning: the fact that you use the same variable names repeatedly doesn't magically turn those variables into "one" thing. There are two variables named l. One exists in main(), one exists in exchange(). The other core thing to understand: Java does call by value; but that means: when passing an array to a method, changes to the parameter are visible to the caller.

But when passing primitive types such as int, the caller does not see updates. See here for all the details.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

Your array has been updated. However you are missing a print statement to print out the updated array.

System.out.println("right order:" + intArray[0] + " " + intArray[1]);

Edit 1: I would suggest that you pencil out the memory model of the array, how it is made available to the exchange method and then this will all be so easy. Remember Java passes by value including references and that array is a reference.

Khanna111
  • 3,627
  • 1
  • 23
  • 25