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];
    }
  }
}
8
  • Use System.out.println(Arrays.toString(karn.toString())); Commented Aug 21, 2017 at 3:05
  • 2
    Your reverse method is also incorrect (it's a noop). Commented Aug 21, 2017 at 3:05
  • 1
    Take a pen and paper to check your logic in first place Commented Aug 21, 2017 at 3:06
  • @ElliottFrisch Good catch...it needs a temp variable to do the swap. Commented Aug 21, 2017 at 3:06
  • how would u guys do it Commented Aug 21, 2017 at 3:11

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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
whats confusing you?
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
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
Maybe the following image 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.
|
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.