在两个阵列中交换两个字符

时间:2013-05-23 07:35:28

标签: java swap chars

所以我有2个char数组和两个随机数,每个数组一个数字。

我希望将char放在第一个数组中随机数的位置,并将其与第二个数组中第二个随机数位置的char交换。我试过这个:

char[] chs1 = {'h', 'e', 'l', 'l', 'o'};
char[] chs2 = {'w', 'o', 'r', 'l', 'd'};
Random rand = new Random();
int ranNum1 = rand.nextInt(chs1.length);
int ranNum2 = rand.nextInt(chs2.length);
chs1[ranNum1] = chs2[ranNum2];
chs2[ranNum2] = chs1[ranNum1];

有谁知道怎么做?

9 个答案:

答案 0 :(得分:4)

你需要一个临时变量来交换:

char temp = chs1[ranNum1];
chs1[ranNum1] = chs2[ranNum2];
chs2[ranNum2] = temp;

答案 1 :(得分:3)

char[] chs1 = {'h', 'e', 'l', 'l', 'o'};
char[] chs2 = {'w', 'o', 'r', 'l', 'd'};
Random rand = new Random();
int ranNum1 = rand.nextInt(chs1.length);
int ranNum2 = rand.nextInt(chs2.length);
char temp = chs1[ranNum1];
chs1[ranNum1] = chs2[ranNum2];
chs2[ranNum2] = temp;

答案 2 :(得分:2)

这不起作用:

chs1[ranNum1] = chs2[ranNum2];
chs2[ranNum2] = chs1[ranNum1];

这是因为在第一个语句结束时,chs1 [ranNum1]与chs2 [ranNum2]相同;所以第二个声明是无操作!即你丢失了最初在chs [ranNum1]中的内容。

你需要一个临时的:

char s = chs1[ranNum1];
chs1[ranNum1] = chs2[ranNum2];
chs2[ranNum2] = s;

答案 3 :(得分:1)

您需要一个临时变量:

char temp = chs1[ranNum1];
chs1[ranNum1] = chs2[ranNum2];
chs2[ranNum2] = temp;

答案 4 :(得分:1)

使用临时变量:

char temp = chs2[ranNum2];
chs2[ranNum2] = chs1[ranNum1];
chs1[ranNum1] = temp;

您所做的是更新一个值,然后获取更新后的值并将其重新放置在原来的位置。

答案 5 :(得分:1)

您应该将至少一个字符存储在临时变量中,如下所示:

char tmp = chs1[ranNum1];
chs1[ranNum1] = chs2[ranNum2];
chs2[ranNum2] = tmp;

答案 6 :(得分:1)

你需要一个临时的char来存储值。因为在你的时刻:chs1 [ranNum1] = chs2 [ranNum2] - 你丢失了chs1 [ranNum1]中的值。

例如:

    chs1[ranNum1] = 'd'
    chs2[ranNum2] = 's'

在分配之后你将不再拥有了。它也将指向s。

所以在分配后

chs1 [ranNum1] ='s' chs2 [ranNum2] ='s'

总而言之:

    char temp = chs1[ranNum1];
    chs1[ranNum1] = chs2[ranNum2];
    chs2[ranNum2] = temp;

答案 7 :(得分:0)

我认为你应该尝试冒泡排序算法。

char[] chs1 = {'h', 'e', 'l', 'l', 'o'};
char[] chs2 = {'w', 'o', 'r', 'l', 'd'};
Random rand = new Random();
int ranNum1 = rand.nextInt(chs1.length);
int ranNum2 = rand.nextInt(chs2.length);
ch temp = chs1[ranNum1];
chs1[ranNum1] = chs2[ranNum2];
chs2[ranNum2] = temp;

答案 8 :(得分:0)

只是为了好玩......绝对是上一个。解决方案有效,但是这个解决方案在没有temp变量的情况下执行相同的工作:

public static void main(String[] argv) {
    char[] a1 = { 'h', 'e', 'l', 'l', 'o' };
    char[] a2 = { 'w', 'o', 'r', 'l', 'd' };
    Random rand = new Random();

    for (int n = 0; n < 2; n++) {
        int indexToSwap = rand.nextInt(a1.length);

        System.out.println("Swap " + indexToSwap);
        System.out.println("Before A1 " + toString(a1) + ", A2 " + toString(a2));

        a1[indexToSwap] ^= a2[indexToSwap];
        a2[indexToSwap] ^= a1[indexToSwap];
        a1[indexToSwap] ^= a2[indexToSwap];

        System.out.println(" After A1 " + toString(a1) + ", A2 " + toString(a2));
    }
}

private static String toString(char[] a) {
    StringBuilder sb = new StringBuilder();
    for (char c : a) {
        sb.append(", ").append(c);
    }
    return "[" + sb.toString().substring(1).trim() + "]";
}

对我而言,它产生了以下输出:

Swap 4
Before A1 [h, e, l, l, o], A2 [w, o, r, l, d]
 After A1 [h, e, l, l, d], A2 [w, o, r, l, o]

Swap 2
Before A1 [w, e, l, l, o], A2 [h, o, r, l, d]
 After A1 [w, e, r, l, o], A2 [h, o, l, l, d]