0

I was trying to use this code I made to reverse a array. I don't understand why I was getting [I@7a84639c as my output in my console.

And for some reason why doesn't this method actually save my reversed array into the array? If i add a print at the bottom of x[i]=c[i]; it shows the array reversed but when i add a call for example karn[0] it shows that my array isn't actually reversed. I want to solve this by staying true to the code i made.

import java.util.Arrays;

public class HelloWorld {
  public static void main(String[] args) {


    int[]karn={1,2,3};

    rev(karn);
    System.out.println(karn.toString());
  }


  public static void rev(int[]x){
    int[]c=new int[x.length];

    for(int i=x.length-1;i>-1;i--){
      c[i]=x[i];
      x[i]=c[i];
    }
  }
}
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131

2 Answers2

2

in your rev method you are using a local variable for c. So this value will not be transferred over to your main method. You must return your array and assign the value to your old array:

public static int[] rev(int[]x){
    //Creates new array this array is different from karn and local to the method.
    //nothing outside of this method can see this array.
    int[]c=new int[x.length];

    for(int i = 0; i < c.length; i++){
        //Here is where we are swapping the values by placing the first
        //value at the last spot of the array and so on
        c[c.length - i - 1] = x[i];
    }
    //we must return the new array we made and assign it to karn so our 
    //changes will be saved and seen outside of the method
    return c;
  }

In main method you must assign the changes of the rev method to karn. You can assign the value and display it like so:

karn = rev(karn);

//for each loop
for(int i : karn)
    System.out.println(i);

//traditional for loop
for(int i = 0; i < karn.length; i++)
    System.out.println(karn[i]);

Arrays do not have a default toString() method. That is why you are seeing the values of the array as you would like. You need to iterate through the array to display them to the console.

Ryan
  • 1,972
  • 2
  • 23
  • 36
  • wow thanks for your answer, but its a bit past my skill level. could you noob it up a bit? ill give you a point regardless for your effort –  Aug 21 '17 at 04:00
  • whats confusing you? – Ryan Aug 21 '17 at 04:00
  • for for loop, im only use to fors in a context of the way i used it . i just want to use the method and then have it changed –  Aug 21 '17 at 04:02
  • I added a traditional for loop to display the values, those two loops provide the same out put they are just two different ways of doing it. I'm about to edit in some more information, it will be out in a few minutes – Ryan Aug 21 '17 at 04:05
  • Maybe the [following image](http://i.imgur.com/m3ccmYs.jpg) helps you to understand the logic. You iterate from **left to right** and always set the element from the **current pointer** (index `i`) to the corresponding position when viewing from **right to left** (index `length - 1 - i`, note that `length - 1` is the last index in the array). @Ryan you may use this to improve your answer and add an explanation of your code. – Zabuzard Aug 21 '17 at 04:08
  • hmm im still confused, idk why. i dont see why my method wouldnt work –  Aug 21 '17 at 04:22
  • whats the simpliest way to reverse iterate over the array –  Aug 21 '17 at 04:25
  • reverse iterate or reverse the values of a array? – Ryan Aug 21 '17 at 04:34
2

Your initial approach looked almost correct, you can do this in-place or through a copy. I posted a comment showing a copy, so I thought I might expand on in-place. I would start with a simple swap method, like

private static void swap(int[] x, int i, int j) {
    if (i != j) {
        int t = x[i];
        x[i] = x[j];
        x[j] = t;
    }
}

Then you only need to iterate the first half of the array, swapping each element with the same index (but from the other half). Like,

public static void rev(int[] x) {
    for (int i = 0; i < x.length / 2; i++) {
        swap(x, i, x.length - i - 1);
    }
}

Then you might call it like

public static void main(String[] args) throws IOException {
    int[] karn = { 1, 2, 3 };
    rev(karn);
    System.out.println(Arrays.toString(karn));
}

for an output of

[3, 2, 1]
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249