小编Diz*_*Diz的帖子

Java反向数组方法

我正在尝试创建一个接收数组然后以相反方式返回该数组的方法.我写的代码反过来返回数组,但是,前两个值现在为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)

java arrays methods reverse

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

arrays ×1

java ×1

methods ×1

reverse ×1