Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rotator<T> {
- public static void main(String[] args) {
- Integer table[] = new Integer[] {1,11,12,11,8};
- Rotator<Integer> rotator = new Rotator<>(table);
- rotator.rotate(3);
- for(Integer i : rotator.tablica) {
- System.out.print(i + " ");
- }
- }
- public T[] tablica;
- public Rotator(T tablica[]) {
- this.tablica=tablica;
- }
- public void rotate(int a) {
- a=a%tablica.length;
- if(a<0)
- throw new IllegalArgumentException("Nie mozna rotowac w lewo");
- else if(a>0)
- rotateRight(a);
- }
- private void rotateRight(int n) {
- while (n > 0) {
- T last = tablica[tablica.length-1];
- for(int i=tablica.length - 1; i>0; i--) {
- tablica[i]=tablica[i - 1];
- }
- tablica[0]=last;
- n--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement