Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ex15 {
- public static void main(String[] args) {
- int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- int[] arr2 = new int[arr1.length];
- printWithSeperatorUsingForLoop(',', arr1);
- printWithSeperatorUsingWhileLoop('-', arr2);
- reverseArrayCopyWithForLoop(arr1, arr2);
- reverseArrayCopyWithWhileLoop(arr1, arr2);
- printWithSeperatorUsingDoWhileLoop(',', arr1);
- printWithSeperatorUsingForLoop('-', arr2);
- }
- private static void printWithSeperatorUsingForLoop(char seperator, int[] array) {
- for(int i = 0; i < array.length; i++) {
- System.out.print(array[i]);
- if(i < array.length - 1) {
- System.out.print(seperator);
- } else {
- System.out.println();
- }
- }
- }
- private static void printWithSeperatorUsingWhileLoop(char seperator, int[] array) {
- int i = 0;
- while(i < array.length) {
- System.out.print(array[i]);
- if(i < array.length - 1) {
- System.out.print(seperator);
- } else {
- System.out.println();
- }
- i++;
- }
- }
- private static void printWithSeperatorUsingDoWhileLoop(char seperator, int[] array) {
- int i = 0;
- do {
- System.out.print(array[i]);
- if(i < array.length - 1) {
- System.out.print(seperator);
- } else {
- System.out.println();
- }
- i++;
- } while(i < array.length);
- }
- private static void reverseArrayCopyWithForLoop(int[] source, int[] destination) {
- for(int i = source.length - 1, j = 0; i >= 0; i--, j++) {
- destination[j] = source[i];
- }
- }
- private static void reverseArrayCopyWithWhileLoop(int[] source, int[] destination) {
- int i = source.length - 1, j = 0;
- while(i >= 0) {
- destination[j] = source[i];
- i--;
- j++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement