New Posts  All Forums:Forum Nav:

Java Array Rotation

post #1 of 5
Thread Starter 
How would I go about creating a method to do the following:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

and switch it around to this:

{4, 5, 6, 7, 8, 9, 10, 1, 2, 3}


I need to utilize the following params: int[] numbers and int distance

Thanks!
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 4200+  ASUS M2N-SLI Deluxe eVGA Geforce 7900GT CO Corsair XMS2 4GB PC2-6400 
Hard DriveOptical DriveOSMonitor
Seagate 160GB SATA2 + Western Digital 250GB SATA2 Lite-On SATA DVD Burner Linux Mint 14 Cinnamon x64   
PowerCase
OCZ GameXStream 700w Antec P180 
  hide details  
Reply
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 4200+  ASUS M2N-SLI Deluxe eVGA Geforce 7900GT CO Corsair XMS2 4GB PC2-6400 
Hard DriveOptical DriveOSMonitor
Seagate 160GB SATA2 + Western Digital 250GB SATA2 Lite-On SATA DVD Burner Linux Mint 14 Cinnamon x64   
PowerCase
OCZ GameXStream 700w Antec P180 
  hide details  
Reply
post #2 of 5
Thread Starter 
Okay here is what I have, but when I input the values, it prints weird characters

Code:
public static void rotate(int[] values, int distance)
    {
        for (int i = 0; i < distance; i++){
            int temp = values[0];
            int j = 0;
            for(j = i; j < distance - 1; j++){
                values[j] = values[j + 1];
            }
        }
        System.out.println(values);
    }
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 4200+  ASUS M2N-SLI Deluxe eVGA Geforce 7900GT CO Corsair XMS2 4GB PC2-6400 
Hard DriveOptical DriveOSMonitor
Seagate 160GB SATA2 + Western Digital 250GB SATA2 Lite-On SATA DVD Burner Linux Mint 14 Cinnamon x64   
PowerCase
OCZ GameXStream 700w Antec P180 
  hide details  
Reply
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 4200+  ASUS M2N-SLI Deluxe eVGA Geforce 7900GT CO Corsair XMS2 4GB PC2-6400 
Hard DriveOptical DriveOSMonitor
Seagate 160GB SATA2 + Western Digital 250GB SATA2 Lite-On SATA DVD Burner Linux Mint 14 Cinnamon x64   
PowerCase
OCZ GameXStream 700w Antec P180 
  hide details  
Reply
post #3 of 5
Look into the modulus operator %.

You can use it to wrap around your array indices and give the desired rotation. I was going to post a snippet, but that would basically solve your problem.
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #4 of 5
I am still studying java, but my solution would be to initialize a second array and fill that from the first array with array location +3
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k Asus P8P67 PRO P67 XFX HD 6850 4 x 2Gb Kingston HyperX 1600MHz 
Hard DriveOSMonitorKeyboard
SSD Intel 80GB + 1TB Samsung HDD Windows 7 64bit 2 x Viewsonic VX2450wm Razer Arctosa 
PowerCaseMouse
Cooler Master Silent Pro 700W Cooler Master Storm Sniper Razer Abyssus 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k Asus P8P67 PRO P67 XFX HD 6850 4 x 2Gb Kingston HyperX 1600MHz 
Hard DriveOSMonitorKeyboard
SSD Intel 80GB + 1TB Samsung HDD Windows 7 64bit 2 x Viewsonic VX2450wm Razer Arctosa 
PowerCaseMouse
Cooler Master Silent Pro 700W Cooler Master Storm Sniper Razer Abyssus 
  hide details  
Reply
post #5 of 5
In c#, close enough to java, im pretty sure java also exposes an array.copy function variation.
Where distance is the amount you wish to rotate, as long as the distance to rotate is less than the length of the numbers array.


http://www.java-samples.com/showtuto...tutorialid=641



Code:
private static int[] rearrangeArray(int[] numbers, int distance)
        {

            int[] temp = new int[numbers.Length];

            Array.Copy(numbers, distance, temp, 0, numbers.Length - distance);
            Array.Copy(numbers, 0, temp, numbers.Length - distance, distance);

            return temp;
        }

Edited by mykah89 - 2/14/11 at 9:17pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming