Reordering an Array

  • tj
  • Newbie
  • Newbie
  • tj
  • Posts: 6

Post 3+ Months Ago

Hi friends,

I need to rorder an array of characters, in java, in a sequence of iterations.

Say, char[] alphabets = { 'a', 'b', 'c', 'd', ..., 'z' }, is the initial setting.

The first iteration should change the array to:
z, a, b, c, d, ..., y

The second:
y, z, a, b, c, d, ..., x

The third:
x, y, z, a, b, c, d, ..., w

etc,...
and the cycle should continue on and on.

Have you guys got any idea how to do this?

Please help!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I think this is what you're looking for:

Code: [ Select ]
public class alpha
{
    public static void main(String[] args)
    {
        char[] alphabets = {'a','b','c','d','e','f','g','h','i','j',
                            'k','l','m','n','o','p','q','r','s','t',
                            'u','v','w','x','y','z'};
        char last;
        
        for(int i=0; i<20; i++)
        {
            last=alphabets[alphabets.length-1];
            System.arraycopy(alphabets, 0, alphabets, 1, alphabets.length-1);
            alphabets[0]=last;
            
            // this is just some code to display the results
            StringBuffer display = new StringBuffer(alphabets.length);
            display.append(alphabets);
            System.out.println(display.toString());    
        }
    }
}
  1. public class alpha
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         char[] alphabets = {'a','b','c','d','e','f','g','h','i','j',
  6.                             'k','l','m','n','o','p','q','r','s','t',
  7.                             'u','v','w','x','y','z'};
  8.         char last;
  9.         
  10.         for(int i=0; i<20; i++)
  11.         {
  12.             last=alphabets[alphabets.length-1];
  13.             System.arraycopy(alphabets, 0, alphabets, 1, alphabets.length-1);
  14.             alphabets[0]=last;
  15.             
  16.             // this is just some code to display the results
  17.             StringBuffer display = new StringBuffer(alphabets.length);
  18.             display.append(alphabets);
  19.             System.out.println(display.toString());    
  20.         }
  21.     }
  22. }


and here is the result of running 20 iterations:

zabcdefghijklmnopqrstuvwxy
yzabcdefghijklmnopqrstuvwx
xyzabcdefghijklmnopqrstuvw
wxyzabcdefghijklmnopqrstuv
vwxyzabcdefghijklmnopqrstu
uvwxyzabcdefghijklmnopqrst
tuvwxyzabcdefghijklmnopqrs
stuvwxyzabcdefghijklmnopqr
rstuvwxyzabcdefghijklmnopq
qrstuvwxyzabcdefghijklmnop
pqrstuvwxyzabcdefghijklmno
opqrstuvwxyzabcdefghijklmn
nopqrstuvwxyzabcdefghijklm
mnopqrstuvwxyzabcdefghijkl
lmnopqrstuvwxyzabcdefghijk
klmnopqrstuvwxyzabcdefghij
jklmnopqrstuvwxyzabcdefghi
ijklmnopqrstuvwxyzabcdefgh
hijklmnopqrstuvwxyzabcdefg
ghijklmnopqrstuvwxyzabcdef
  • tj
  • Newbie
  • Newbie
  • tj
  • Posts: 6

Post 3+ Months Ago

Thankyou Rich, :)

Thats exactly what I was looking for.
Is it possible to do the same thing on a String? (not array!)
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

The String class doesn't really provide any handy methods for doing this and I can't think of any way to do it directly on a string. Strings are immutable in Java, which is why all the string methods like myString.trim() actually return a new string object. Usually, you just assign the returned value back to the original variable (myString = myString.trim()), so it looks like the contents have been operated on, but it actually creates a new modified string.

The ability to convert a string to a char array and initialize a new string from a char array makes me think that using a method to do the reordering or shifting on a char array and then calling it with either a plain char array like alphabets or a string (by calling with links.toCharArray) would allow us to do the same thing with one method. The only difference would be that when we called it with a string we would invoke the string's toCharArray method and use the return value to create a new string that we would assign back to the links variable.


Code: [ Select ]
public class alpha
{
    public static void main(String[] args)
    {
        char[] alphabets = {'a','b','c','d','e','f','g','h','i','j',
                   'k','l','m','n','o','p','q','r','s','t',
                   'u','v','w','x','y','z'};
                  
        String links = "abcdefghijklmnopqrstuvwxyz";
        
        // send links as a char array to the shift method
        // and use the returned char array to make a new String
        links = new String(shiftRight(links.toCharArray(),3));
        
        // send alphabets char array to the shift method
        // and assign the return value directly to alphabets
        alphabets=shiftRight(alphabets,3);
                     
        // this is just some code to display the results
        StringBuffer display = new StringBuffer(alphabets.length);
        display.append(alphabets);
        System.out.println("char array = " + display.toString());
        System.out.println("string = " + links);
    }
     
    public static char[] shiftRight(char[] arrayToShift, int numShifts)
    {
        char last;
        for(int i=0; i<numShifts; i++)
        {
            last=arrayToShift[arrayToShift.length-1];
            System.arraycopy(arrayToShift, 0, arrayToShift, 1, arrayToShift.length-1);
            arrayToShift[0]=last;
        }
        return arrayToShift;         
    }
    
}
                
  1. public class alpha
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         char[] alphabets = {'a','b','c','d','e','f','g','h','i','j',
  6.                    'k','l','m','n','o','p','q','r','s','t',
  7.                    'u','v','w','x','y','z'};
  8.                   
  9.         String links = "abcdefghijklmnopqrstuvwxyz";
  10.         
  11.         // send links as a char array to the shift method
  12.         // and use the returned char array to make a new String
  13.         links = new String(shiftRight(links.toCharArray(),3));
  14.         
  15.         // send alphabets char array to the shift method
  16.         // and assign the return value directly to alphabets
  17.         alphabets=shiftRight(alphabets,3);
  18.                      
  19.         // this is just some code to display the results
  20.         StringBuffer display = new StringBuffer(alphabets.length);
  21.         display.append(alphabets);
  22.         System.out.println("char array = " + display.toString());
  23.         System.out.println("string = " + links);
  24.     }
  25.      
  26.     public static char[] shiftRight(char[] arrayToShift, int numShifts)
  27.     {
  28.         char last;
  29.         for(int i=0; i<numShifts; i++)
  30.         {
  31.             last=arrayToShift[arrayToShift.length-1];
  32.             System.arraycopy(arrayToShift, 0, arrayToShift, 1, arrayToShift.length-1);
  33.             arrayToShift[0]=last;
  34.         }
  35.         return arrayToShift;         
  36.     }
  37.     
  38. }
  39.                 


the result of running this with 3 shifts was:

char array = xyzabcdefghijklmnopqrstuvw
string = xyzabcdefghijklmnopqrstuvw
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

If you didn't want to use a method you could just do it like this.
Code: [ Select ]
public class alpha
{
    public static void main(String[] args)
    {
        int numShifts = 3;          
        String links = "abcdefghijklmnopqrstuvwxyz";
        char[] temp = links.toCharArray();
        
        char last;
        for(int i = 0; i < numShifts; i++)
        {
            last=temp[temp.length-1];
            System.arraycopy(temp, 0, temp, 1, temp.length-1);
            temp[0]=last;
        }
        
        links = new String(temp);        

        System.out.println("string = " + links);
    }
}
  1. public class alpha
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         int numShifts = 3;          
  6.         String links = "abcdefghijklmnopqrstuvwxyz";
  7.         char[] temp = links.toCharArray();
  8.         
  9.         char last;
  10.         for(int i = 0; i < numShifts; i++)
  11.         {
  12.             last=temp[temp.length-1];
  13.             System.arraycopy(temp, 0, temp, 1, temp.length-1);
  14.             temp[0]=last;
  15.         }
  16.         
  17.         links = new String(temp);        
  18.         System.out.println("string = " + links);
  19.     }
  20. }

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: ScottG and 76 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.