Array

26. Program

(Imp_Q) Write a program for left shift by 2 elements in array ?

class J { public static void main(String args[]) { int[] a = {2, 4, 4, 2, 4, 7, 4, 5}; for(int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); } System.out.println(); for(int i = 2 ; i < a.length; i++) { a[i-2] = a[i]; } for(int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); } System.out.println(); } }

27. Program

(Imp_Q) Write a program for left rotate by 2 elements in array ?

class K { public static void main(String args[]) { int[] a = {2, 4, 4, 2, 4, 7, 4, 5}; for(int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); } int temp1 = a[0]; int temp2 = a[1]; System.out.println(); for(int i = 2 ; i < a.length; i++) { a[i-2] = a[i]; } a[a.length-2] = temp1; a[a.length-1] = temp2; for(int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); } System.out.println(); } }

28. Program

(Imp_Q) Write a program for right shift by 2 elements in array ?

class L { public static void main(String args[]) { int[] a = {2, 4, 4, 2, 4, 7, 4, 5}; for(int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); } System.out.println(); for(int i = a.length-1; i > 1; i--) { a[i] = a[i-2]; } for(int i = 0; i < a.length; i++) { System.out.print(a[i] + ","); } System.out.println(); } }

Page....