Java 在两个索引之间反转数组的一部分

Java 在两个索引之间反转数组的一部分,java,arrays,Java,Arrays,我试图编写一个方法,在两个特定索引之间反转数组中的输入。但是,它会不断返回原始数组,就好像在测试时没有任何更改一样。有什么想法吗 public static void reverse (char[] ar, int i, int j) { char[] arTwo= new char[ar.length]; for (int x =0; x < ar.length; x++){ arTwo[x]= ar[x]; } int up

我试图编写一个方法,在两个特定索引之间反转数组中的输入。但是,它会不断返回原始数组,就好像在测试时没有任何更改一样。有什么想法吗

public static void reverse (char[] ar, int i, int j) {
    char[] arTwo= new char[ar.length]; 
    for (int x =0; x < ar.length; x++){     
        arTwo[x]= ar[x];
    }
    int up =i;
    int down = j;
    while (up> j ) {
        ar[up] = arTwo[down];
        up++;
        down--;
    }

}
publicstaticvoidreverse(char[]ar,inti,intj){
char[]arTwo=新字符[ar.length];
对于(int x=0;xj){
ar[上升]=arTwo[下降];
up++;
向下--;
}
}

为什么不交换原始数组中的项

for (int x=0; x<(j-i)/2; x++)
{
    int index1 = i+x;
    int index2 = j-x;
    char temp = ar[index1];
    ar[index1] = ar[index2];
    ar[index2] = temp;
}

for(int x=0;x为什么不交换原始数组中的项

for (int x=0; x<(j-i)/2; x++)
{
    int index1 = i+x;
    int index2 = j-x;
    char temp = ar[index1];
    ar[index1] = ar[index2];
    ar[index2] = temp;
}

for(int x=0;xwhile
在条件为
true
时循环,但假设
i
up>j
从一开始就是
false
,因此不会发生迭代

试一试

while(向上<向下){

<> >代码> Up> <代码>在<>代码> >代码>中间。< <代码> > 循环循环,条件是“代码>真< /代码>,但假设<>代码> i 代码>,<代码> UP > j>代码>从一开始就是<代码> false <代码>,这样就不会发生迭代。

试一试

while(向上<向下){

<>代码> Up/<代码>在中间遇到<代码>向下/代码>。什么是变量<代码> CONTUP ?这是糟糕的编辑,应该是不可计数的。你的while循环不应该有条件<代码> UPJ < /代码>吗?什么是变量<代码>
upj