Guest User

Untitled

a guest
Nov 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. package array;
  2.  
  3. public class ArrayReversalOneByOne {
  4. public static void main (String[] args) throws java.lang.Exception
  5. {
  6. int[] arr = {1,2,3,4,5,6,7};
  7. int d = 2;
  8. reverseArray(arr,d);
  9. for(int i = 0; i < arr.length;i++){
  10. System.out.println(arr[i]);
  11. }
  12. }
  13.  
  14. public static void reverseArray(int arr[], int d){
  15. if(d==0){
  16. return;
  17. }
  18. for(int i = 0; i<d;i++) {
  19. reverseArrayOneByOne(arr);
  20. }
  21. }
  22.  
  23. private static void reverseArrayOneByOne(int[] arr) {
  24. int temp = arr[0];
  25. for (int i=1;i<arr.length;i++) {
  26. arr[i-1] = arr[i];
  27. }
  28. arr[arr.length-1]=temp;
  29. }
  30. }
Add Comment
Please, Sign In to add comment