Advertisement
GieeF

Untitled

Jun 16th, 2020
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public class Rotator<T> {
  2.  
  3. public static void main(String[] args) {
  4. Integer table[] = new Integer[] {1,11,12,11,8};
  5. Rotator<Integer> rotator = new Rotator<>(table);
  6. rotator.rotate(3);
  7. for(Integer i : rotator.tablica) {
  8. System.out.print(i + " ");
  9. }
  10. }
  11.  
  12. public T[] tablica;
  13. public Rotator(T tablica[]) {
  14. this.tablica=tablica;
  15. }
  16. public void rotate(int a) {
  17. a=a%tablica.length;
  18. if(a<0)
  19. throw new IllegalArgumentException("Nie mozna rotowac w lewo");
  20. else if(a>0)
  21. rotateRight(a);
  22. }
  23. private void rotateRight(int n) {
  24. while (n > 0) {
  25. T last = tablica[tablica.length-1];
  26. for(int i=tablica.length - 1; i>0; i--) {
  27. tablica[i]=tablica[i - 1];
  28. }
  29. tablica[0]=last;
  30. n--;
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement