Вопрос

So I have 2 char arrays and two random numbers, one number for each array.

I want to get the char in the position of the random number in the first array, and swap that around with the char in the position of the second random number in the second array. I tried this:

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];

Does anyone know how to?

Это было полезно?

Решение

You need a temp variable to swap:

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

Другие советы

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;

This is not going to work:

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

This is because at the end of the first statement, chs1[ranNum1] is the same as chs2[ranNum2]; so the second statement is a no-op! i.e. you've lost what was originally in chs[ranNum1].

You need a temporary:

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

You need a temporary variable:

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

Use a temporary variable:

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

What you did is updating one value, and than get the updated value and put it again where it has already been.

You should store at least one of the chars in a temp variable, like so:

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

You need a temporary for a char, to store the value. Because at the moment when you: chs1[ranNum1] = chs2[ranNum2] - Your losing the value which was in chs1[ranNum1].

For example:

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

after the assign you won't have d anymore. It will also point to s.

So after the assign

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

So all in all:

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

I think you should try bubble sort algorithm.

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;

Just for fun... Definitely prev. solutions work, but this one do the same job without temp variable:

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() + "]";
}

For me it produced following output:

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]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top