Link to home
Start Free TrialLog in
Avatar of jovanvliet
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];//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];
       
    }
//--- 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]; 
       
    }

Open in new window

Avatar of a_b
a_b

Try using the collections.sort() method.
Sorry, I meant Collections.shuffle();
ASKER CERTIFIED SOLUTION
Avatar of a_b
a_b

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
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.

Avatar of jovanvliet

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


[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 ); 
      
    }

Open in new window

You should try a few sort algorithms and you would come to know how each one behaves and start using the best one.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
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;
}

Open in new window

:-)