-2

Need a method that returns the array with the order of the numbers reversed.

For example, if the input array is {5,6,8} the method returns {8,6,5}

This is what I have done. I get an error message saying temp cannot be resolved.

public int[] reverseData (int[] validData) {

for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
ali
  • 11
  • 5
  • http://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java – martinez314 Apr 29 '13 at 19:45
  • 2
    change `i < validData.length` to `i < validData.length/2` – mishadoff Apr 29 '13 at 19:45
  • @thegrinner: It's in the title. – Robert Harvey Apr 29 '13 at 19:45
  • @RobertHarvey I was hoping for something more specific, like "But it throws error X" or "Input X gives me output Y, which is wrong." – thegrinner Apr 29 '13 at 19:47
  • @thegrinner: If we required all questions to be of that form, there wouldn't be any useful information on Stack Overflow at all, but only endless parades of "help me solve my highly-localized problem" questions (we're almost at that point now). – Robert Harvey Apr 29 '13 at 19:53
  • @RobertHarvey I hadn't thought of it that way. My concern was in the opposite direction - fear that it was too vague (like "How do I use library XYZ"). – thegrinner Apr 29 '13 at 19:57

1 Answers1

1

You should only do that for half of the array. If you swap each pair twice, you'll end up with the exact same array. So, use

 for(int i = 0; i < validData.length/2; i++)

instead