Guest User

Untitled

a guest
Jul 8th, 2010
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3.  
  4. public class ArrayElementShifter {
  5.  
  6.     private static boolean printDebug = true;
  7.    
  8.     private static int dimension = 10;
  9.    
  10.     private static int n = 3;
  11.    
  12.     /**
  13.      * @param args
  14.      */
  15.     public static void main(String[] args) {
  16.        
  17.         //array initialization
  18.         Integer[] initArray = new Integer[dimension];
  19.         for(int i=0; i<dimension; i++) initArray[i] = i+1;
  20.        
  21.         //Move N elements to the end and print results
  22.         System.out.println("\nResult: " + Arrays.toString(moveNElementsToTheEnd(initArray, n)));
  23.     }
  24.    
  25.     public static final Object[] moveNElementsToTheEnd(Object[] initArray, int n){
  26.        
  27.         //for index calculation simplicity natural numbers (1,2,3...) are used;
  28.         //for addressing array elements index-1 is used
  29.        
  30.         if(initArray.length % n != 0){
  31.             int tmpIdx = 1, freeCellIdx = 1, nextValueIdx = 0;
  32.             Object tmp = initArray[freeCellIdx -1];
  33.            
  34.             for(int i=0; i<initArray.length; i++){
  35.                 nextValueIdx = (freeCellIdx + n > initArray.length ? freeCellIdx - initArray.length : freeCellIdx) + n;
  36.                 if(nextValueIdx == tmpIdx){
  37.                     initArray[freeCellIdx-1] = tmp;
  38.                 } else{
  39.                     initArray[freeCellIdx-1] = initArray[nextValueIdx-1];
  40.                 }
  41.            
  42.                 freeCellIdx = nextValueIdx;
  43.    
  44.                 if(printDebug) {
  45.                     System.out.println("Debug:" + Arrays.toString(initArray));
  46.                 }
  47.             }
  48.         } else {
  49.             int i =0;
  50.             while(i<n){
  51.                 Object tmp = initArray[i];
  52.                 int j=0;
  53.                 while(i+n*(j+1) < initArray.length){
  54.                     initArray[i+n*j] = initArray[i+n*(j+1)];
  55.                     j++;
  56.                     if(printDebug) {
  57.                         System.out.println("Debug:" + Arrays.toString(initArray));
  58.                     }
  59.                 }
  60.                 initArray[i+n*j] = tmp;
  61.                 i++;
  62.                 if(printDebug) {
  63.                     System.out.println("Debug:" + Arrays.toString(initArray));
  64.                 }
  65.             }
  66.         }
  67.        
  68.         return initArray;
  69.     }
  70. }
  71.  
  72. Dim: 10; N:3
  73.  
  74. Debug:[4, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  75. Debug:[4, 2, 3, 7, 5, 6, 7, 8, 9, 10]
  76. Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 10]
  77. Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 3]
  78. Debug:[4, 2, 6, 7, 5, 6, 10, 8, 9, 3]
  79. Debug:[4, 2, 6, 7, 5, 9, 10, 8, 9, 3]
  80. Debug:[4, 2, 6, 7, 5, 9, 10, 8, 2, 3]
  81. Debug:[4, 5, 6, 7, 5, 9, 10, 8, 2, 3]
  82. Debug:[4, 5, 6, 7, 8, 9, 10, 8, 2, 3]
  83. Debug:[4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
  84.  
  85. Result: [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
  86.  
  87. Dim: 13; N:3
  88.  
  89. Debug:[4, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  90. Debug:[4, 2, 3, 7, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  91. Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 10, 11, 12, 13]
  92. Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 13, 11, 12, 13]
  93. Debug:[4, 2, 3, 7, 5, 6, 10, 8, 9, 13, 11, 12, 3]
  94. Debug:[4, 2, 6, 7, 5, 6, 10, 8, 9, 13, 11, 12, 3]
  95. Debug:[4, 2, 6, 7, 5, 9, 10, 8, 9, 13, 11, 12, 3]
  96. Debug:[4, 2, 6, 7, 5, 9, 10, 8, 12, 13, 11, 12, 3]
  97. Debug:[4, 2, 6, 7, 5, 9, 10, 8, 12, 13, 11, 2, 3]
  98. Debug:[4, 5, 6, 7, 5, 9, 10, 8, 12, 13, 11, 2, 3]
  99. Debug:[4, 5, 6, 7, 8, 9, 10, 8, 12, 13, 11, 2, 3]
  100. Debug:[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 11, 2, 3]
  101. Debug:[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3]
  102.  
  103. Result: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3]
  104.  
  105. Dim: 8; N:4
  106.  
  107. Debug:[5, 2, 3, 4, 5, 6, 7, 8]
  108. Debug:[5, 2, 3, 4, 1, 6, 7, 8]
  109. Debug:[5, 6, 3, 4, 1, 6, 7, 8]
  110. Debug:[5, 6, 3, 4, 1, 2, 7, 8]
  111. Debug:[5, 6, 7, 4, 1, 2, 7, 8]
  112. Debug:[5, 6, 7, 4, 1, 2, 3, 8]
  113. Debug:[5, 6, 7, 8, 1, 2, 3, 8]
  114. Debug:[5, 6, 7, 8, 1, 2, 3, 4]
  115.  
  116. Result: [5, 6, 7, 8, 1, 2, 3, 4]
Advertisement
Add Comment
Please, Sign In to add comment