我正在尝试创建一个接收数组然后以相反方式返回该数组的方法.我写的代码反过来返回数组,但是,前两个值现在为0.任何人都知道我做错了什么?
public static int[] reverse(int[] x)
{
int []d = new int[x.length];
for (int i = 0; i < x.length/2; i++) // for loop, that checks each array slot
{
d[i] = x[i];
x[i] = x[x.length-1-i]; // creates a new array that is in reverse order of the original
x[x.length-1-i] = d[i];
}
return d; // returns the new reversed array
}
Run Code Online (Sandbox Code Playgroud)