0

My code is returning gibberish(at least for me) and I don't know how to get it to work.

I've tried some different return configurations but none of them work the way I want it to.

class Main {
  public static void main(String[] args) {
    int[] reversing= {1,5,3,14,5,26,7,8,9,10,928,0};
      System.out.println(reverse(reversing));
  }
  public static int[] reverse(int[] nums){
    for(int i=0;i<nums.length;i++){
      nums[i]=nums[nums.length-1];
    }
    return nums;
  }
}

The code is supposed to take an array of integers and reverse the numbers but instead it simply returns [I@5acf9800

1 Answers1

1

System.out.println(<>) prints the hashcode of the int[] array object, not the elements of the array.

You need to iterate over a print statement through the array. But besides that, your method doesn't actually reverse the arrays.

public class Reverser {
public static void main(String[] args) {
    int[] reversing = { 1, 5, 3, 14, 5, 26, 7, 8, 9, 10, 928, 0 };
    for (int i : reverse(reversing)) System.out.println(i);
}

public static int[] reverse(int[] nums) {
    int [] reversed = new int[nums.length];
    int reverseIndex = nums.length - 1;
    for (int i = 0; i < nums.length; i++) {
        reversed[reverseIndex--] = nums[i];
    }
    return reversed;
}

}

Jeff Stewart
  • 125
  • 5