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

1 回答 1

1

您应该只对阵列的一半执行此操作。如果将每对交换两次,您将得到完全相同的数组。所以,使用

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

反而

于 2013-04-29T19:46:11.880 回答