java - 反转两个索引之间的数组部分

标签 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 =i;
    int down = j;
    while (up> j ) {
        ar[up] = arTwo[down];
        up++;
        down--;
    }

}

最佳答案

while当条件为true时循环循环,但假设i < j , up > jfalse从一开始就不会发生迭代。

尝试

while (up < down) {

所以up满足down在中间。

关于java - 反转两个索引之间的数组部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21916691/

相关文章:

java - 我试图捕获 NumberFormatException 但异常仍在传递

java - 使用 Android 版 Youtube API 在应用程序中显示 YouTube 视频

python如何用计数器值填充二维数组

javascript - 如何根据公共(public)属性将数组中的对象分组为数组的数组

java - 尝试使用 Intellij 连接到在线数据库

java - Python - 我如何读入和读出以便其他读者可以阅读它以进一步追求?

java - 尝试在 Java 中创建 json 对象但获取嵌套数组?如何以更好的方式做到这一点?

c++ - 在 int 数组中查找连续值的运行

arrays - 数组内的 C 联合多态性

java - Spring Boot Security如何检查/验证访问 token