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;