0

Complete the reverseValues method which is passed an array of ints(values) as a parameter. The method returns the ints(values) with the order of the numbers reversed.

For example, if the input array is {3, 7, 2, 4}

the method returns {4,2,7,3}

An example: reverseValues({3, 7, 2, 4}) should return {4,2,7,3}

I can't find any help that shows how to do this without seeing the array before hand.

2 Answers2

2
for(int i =0; i < array.length/2; i++){
   int temp = array[i];
   array[i] = array[array.length-1 - i];
   array[array.length-1 - i] = temp;
}
munch1324
  • 1,148
  • 5
  • 10
-1

Have you tried looking at trying to convert the array to a list, and then call:

 public ArrayList<String> reverse(ArrayList<String> list) {
     ArrayList<String> result = new ArrayList<String>(list.size());

    for(int i=list.size()-1;i>=0;i--) {
       result.add(list.get(i));
    }    

    return result;
}

Something like this should work. Might not be the fastest, but it will reverse the list.

Erik Pragt
  • 13,513
  • 11
  • 58
  • 64