Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将char数组中的元素切换到int数组中的相应位置_Java_Arrays_Char_Int - Fatal编程技术网

Java 将char数组中的元素切换到int数组中的相应位置

Java 将char数组中的元素切换到int数组中的相应位置,java,arrays,char,int,Java,Arrays,Char,Int,我试图将字母数组中的元素切换到位置数组中相应的位置。元素正在交换,但位置不正确。我这样做是完全错误的还是我的逻辑有问题 public class Words { public static void main (String args[]) { char letters[] = {'l', 'o', 'e', 'h', 'l'}; int positions[] = {2, 4, 1, 0, 3}; int limit1 = le

我试图将字母数组中的元素切换到位置数组中相应的位置。元素正在交换,但位置不正确。我这样做是完全错误的还是我的逻辑有问题

public class Words
{
    public static void main (String args[])
    {
        char letters[] = {'l', 'o', 'e', 'h', 'l'};
        int positions[] = {2, 4, 1, 0, 3};

        int limit1 = letters.length;
        int temp1;
        char temp2;
        for(int i = 0; i < letters.length; i++)
        {
            limit1--;
            for(int j = 0; j < limit1; j++)
            {
                temp1 = positions[j];
                temp2 = letters[temp1];
                letters[temp1] = letters[j];
                letters[j] = temp2;
            }
        }
        for(int i = 0; i < letters.length; i++)
        {
            System.out.print(letters[i] + " ");
        }
    }
}

您的代码错误:

 ...
 temp2 = letters[temp1]; // temp2 is a letter now 
 ...
 letters[temp2] = letters[j]; // the index you are using is a unicode char

问题解决了,有一种更简单的方法可以创建一个数组,并在给定的位置为其指定元素。这是一个简单得多的方法


如果您不必在适当的位置执行此操作,只需创建一个新阵列就会容易得多

char[] letters = {'l', 'o', 'e', 'h', 'l'};
int[] positions = {2, 4, 1, 0, 3};

char[] result = new char[letters.length];
for (int i = 0; i < letters.length; i++)
    result[positions[i]] = letters[i];

System.out.println(result);
对于就地解决方案,可以执行以下操作。输出与上面相同

char letters[] = {'l', 'o', 'e', 'h', 'l'};
int positions[] = {2, 4, 1, 0, 3};

for (int i = 0, j; i < letters.length; i++) {
    while ((j = positions[i]) != i) {
        char tempLetter = letters[i];
        letters[i] = letters[j];
        letters[j] = tempLetter;
        int tempPosition = positions[i];
        positions[i] = positions[j];
        positions[j] = tempPosition;
    }
}

System.out.println(letters);
只需使用一个循环即可轻松编写

for (int i = 0, j; i < letters.length; ) {
    if ((j = positions[i]) == i) {
        i++;
    } else {
        char tempLetter = letters[i];
        letters[i] = letters[j];
        letters[j] = tempLetter;
        int tempPosition = positions[i];
        positions[i] = positions[j];
        positions[j] = tempPosition;
    }
}

temp2=字母[temp1]表示将例如“l”赋值给temp2,l的/Unicode值为108。然后你在做字母[temp2],也就是字母[108],但是字母只有5长,所以你会有例外。-看看j循环中的代码,我对你到底想做什么感到困惑。使用temp变量使它看起来像是您在尝试交换,但肯定没有交换。是的,我正在尝试交换,感谢您帮助我实现id分配temp2作为int而不是char。我现在看到我在循环中的最后一个条件也是错误的。我将编辑代码,向您展示它已被更改的内容。一旦您交换了一对字母,位置数组将与字母数组不同步。-您需要在适当的位置执行此操作吗?因为如果你能为结果创建一个新的数组,那就容易多了。-另外,重新考虑你的逻辑。这可以使用单个循环来完成。嵌套循环是不需要的。哦,好的,所以我创建了一个单独的数组,并将字母分配到所需的位置,无需更改给定的数组。这对我来说是有意义的是的,我编辑了代码,因为我没听清楚。我将temp2指定为char,并更改了循环中的一行代码。我认为这个解决方案太明显和简单了,真正的挑战是在适当的位置执行它。
for (int i = 0, j; i < letters.length; ) {
    if ((j = positions[i]) == i) {
        i++;
    } else {
        char tempLetter = letters[i];
        letters[i] = letters[j];
        letters[j] = tempLetter;
        int tempPosition = positions[i];
        positions[i] = positions[j];
        positions[j] = tempPosition;
    }
}