Advertisement
Guest User

Untitled

a guest
May 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class ShiftZeroPositionElementInArrayDemo{
  4.  
  5.     public static void main (String[] args){
  6.  
  7.         String[] array = {"*", "a", "b", "c", "d", "e", "f", "g", "h", "i"};
  8.        
  9.         Scanner input = new Scanner(System.in);
  10.         System.out.print("Enter an offset: ");
  11.         int offset = input.nextInt();
  12.  
  13.         if(offset >= array.length){
  14.             System.exit(0);
  15.         }
  16.  
  17.         //store the data that's of interest to us
  18.         String pivot = array[0];
  19.  
  20.         //array element shift
  21.         for(int i = 0; i < offset; i++){
  22.             array[i] = array[i+1];
  23.         }
  24.  
  25.         //swap
  26.         array[offset] = pivot;
  27.  
  28.         //and finally iterate through the array to print it
  29.         for(int i = 0; i < array.length; i++){
  30.             System.out.print(array[i]);
  31.             System.out.print(" ");
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement