jovanvliet
asked on
shuffling a character array in Java
Hi,
I am a beginning student in Java and would really appreciate any help you can give me.
As part of review program preparing for an exam, I am trying to randomize an array of letters of the alphabet. My code does randomize the letters but repeats some of them in the new array. Could you please tell me what I am doing wrong? I only want each letter to appear once in the randomLetters array. Many thanks! Jo
//--- Shuffle by exchanging each element randomly
char randomLetters[]; //initialize array for shuffled letters
randomLetters=new char[letters.length];//all ocate memory space same length as original array of the alphabet array
for (int i=0; i< letters.length; i++) { //actually copies each letter from letters[i] into randomLetters[i] instead of just pointing/referencing
randomLetters[i]=letters[i ];
}
for (int i=0; i<randomLetters.length; i++) {
int randomLetter = randomGenerator.nextInt(ra ndomLetter s.length);
char temp = randomLetters[randomLetter ];// holds temp value of letter - used below
randomLetters[randomLetter ]= randomLetters[i];
randomLetters[i]=temp;//th is puts letter/number that was held back into scrambled array
randomLetters[i] = randomLetters[randomLetter ];
}
I am a beginning student in Java and would really appreciate any help you can give me.
As part of review program preparing for an exam, I am trying to randomize an array of letters of the alphabet. My code does randomize the letters but repeats some of them in the new array. Could you please tell me what I am doing wrong? I only want each letter to appear once in the randomLetters array. Many thanks! Jo
//--- Shuffle by exchanging each element randomly
char randomLetters[]; //initialize array for shuffled letters
randomLetters=new char[letters.length];//all
for (int i=0; i< letters.length; i++) { //actually copies each letter from letters[i] into randomLetters[i] instead of just pointing/referencing
randomLetters[i]=letters[i
}
for (int i=0; i<randomLetters.length; i++) {
int randomLetter = randomGenerator.nextInt(ra
char temp = randomLetters[randomLetter
randomLetters[randomLetter
randomLetters[i]=temp;//th
randomLetters[i] = randomLetters[randomLetter
}
//--- Shuffle by exchanging each element randomly
char randomLetters[]; //initialize array for shuffled letters
randomLetters=new char[letters.length];//allocate memory space same length as original array of the alphabet array
for (int i=0; i< letters.length; i++) { //actually copies each letter from letters[i] into randomLetters[i] instead of just pointing/referencing
randomLetters[i]=letters[i];
}
for (int i=0; i<randomLetters.length; i++) {
int randomLetter = randomGenerator.nextInt(randomLetters.length);
char temp = randomLetters[randomLetter];// holds temp value of letter - used below
randomLetters[randomLetter]= randomLetters[i];
randomLetters[i]=temp;//this puts letter/number that was held back into scrambled array
randomLetters[i] = randomLetters[randomLetter];
}
Try using the collections.sort() method.
Sorry, I meant Collections.shuffle();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You'd probably be better off with a Collection of single letter String. Then you can shuffle it more easrily
There are actually 100 ways of doing a program. But as a beginner you should try to get doing things in a better and optimized way in the first place itself, so in the long run you would get the habit of that.
First of all, why do you need a temp array.
And as suggested by others, do you want to use Collections or you want to try it first with array itself.
First of all, why do you need a temp array.
And as suggested by others, do you want to use Collections or you want to try it first with array itself.
ASKER
CPColin,
Thank you for letting me know I had a redundant line of code!
a_b
Thank you for the Knuth info and code.
I tried it and got a DrJava error message:
"cannot find symbol method randomGenerator(int,int)"
I very ignorant. Do I need to create a method or import something?
I have put the revised code below in case you have time to look at it.
Thanks!
Jo
Thank you for letting me know I had a redundant line of code!
a_b
Thank you for the Knuth info and code.
I tried it and got a DrJava error message:
"cannot find symbol method randomGenerator(int,int)"
I very ignorant. Do I need to create a method or import something?
I have put the revised code below in case you have time to look at it.
Thanks!
Jo
[code]
import java.util.Random;
Random randomGenerator = new Random();
int rand;
for (int i=26; i>=0; i--) {
int min=0;
int max=i;
rand=randomGenerator(0,i);
swap(letters[i], letters[rand]);
System.out.println(letters );
}
You should try a few sort algorithms and you would come to know how each one behaves and start using the best one.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh! Thanks you. you're very patient!
May I ask one more question???
I now get an error
cannot find symbol method swap(char, char)
for this line of code
swap(letters[i], letters[rand]);
I was trying to follow the
swap(pArr[i], pArr[rand]);
in the Knuth example
How did I mess this up?
Thanks,
Jo
(We study Collections and Lists in a few weeks but haven't gotten there yet so we ar to use arrays)
May I ask one more question???
I now get an error
cannot find symbol method swap(char, char)
for this line of code
swap(letters[i], letters[rand]);
I was trying to follow the
swap(pArr[i], pArr[rand]);
in the Knuth example
How did I mess this up?
Thanks,
Jo
(We study Collections and Lists in a few weeks but haven't gotten there yet so we ar to use arrays)
You need to copy the swap method from that code into yours
Actually you'd be better off implementing swap like this:
private void swap (char[] array, int index1, int index2) {
char temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
:-)