简体   繁体   中英

java arrays reverse not understanding the logic

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];
    }
  }
}

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM