我正在尝试编写一种方法来反转两个特定索引之间的数组中的输入。但是,它不断返回原始数组,就好像测试时没有任何变化一样。有什么想法吗?
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--;
}
}
最佳答案
while
当条件为true
时循环循环,但假设i < j
, up > j
是 false
从一开始就不会发生迭代。
尝试
while (up < down) {
所以up
满足down
在中间。
关于java - 反转两个索引之间的数组部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21916691/