// This class contains several small examples of array manipulation. Each is // written as a method. The main method includes some sample client code. import java.util.*; public class ArraySample3 { public static void main(String[] args) { int[] list1 = {8, 12, 4, 0, 208, 14, 42, 38, 72}; int[] list2 = {42, 84, 92, 96, 2, 4, 8, 0, 72, 302, 17, 2, 408}; int[] list3 = {-8, 0, 45, 103, 982}; System.out.println("list1 = " + Arrays.toString(list1)); System.out.println("list2 = " + Arrays.toString(list2)); System.out.println("list3 = " + Arrays.toString(list3)); System.out.println("isSorted(list1) returns " + isSorted(list1)); System.out.println("isSorted(list2) returns " + isSorted(list2)); System.out.println("isSorted(list3) returns " + isSorted(list3)); System.out.println("result of rotating right:"); System.out.println(" list3 = " + Arrays.toString(list3)); for (int i = 0; i < list3.length; i++) { rotateRight(list3); System.out.println(" list3 = " + Arrays.toString(list3)); } int[] result1 = interleave(list1, list2); int[] result2 = interleave(list2, list1); System.out.println("interleave(list1, list2) ="); System.out.println(Arrays.toString(result1)); System.out.println("interleave(list2, list1) ="); System.out.println(Arrays.toString(result2)); } // Write a static method called isSorted that takes an array of integers as // a parameter and that returns true if the integers appear in sorted // (nondecreasing) order public static boolean isSorted(int[] list) { for (int i = 0; i < list.length - 1; i++) { if (list[i] > list[i + 1]) { return false; } } return true; } // Write a static method called rotateRight that takes an array of integers // as a parameter and that rotates all values to the right by one position, // rotating the last value to the front of the array (e.g., given the list // {1, 2, 3, 4}, a call on rotateRight should yield the list {4, 1, 2, 3}) public static void rotateRight(int[] list) { int temp = list[list.length - 1]; for (int i = list.length - 2; i >= 0; i--) { list[i + 1] = list[i]; } list[0] = temp; } // Write a static method called interleave that takes two arrays of // integers as parameters and that returns a new array that contains // the result of interleaving the elements of the two arrays. Two // arrays are interleaved by taking elements in an alternating // fashion from the two lists (first value of first list, first // value of second list, second value of first list, second value of // second list, etc). If one list is longer than the other, ignore // the extra values. // Write a static method called interleave that takes two arrays of // integers as parameters and that returns a new array that contains // the result of interleaving the elements of the two arrays. Two // arrays are interleaved by taking elements in an alternating // fashion from the two lists (first value of first list, first // value of second list, second value of first list, second value of // second list, etc). If one list is longer than the other, ignore // the extra values. public static int[] interleave(int[] list1, int[] list2) { int min = Math.min(list1.length, list2.length); int[] result = new int[2 * min]; for (int i = 0; i < min; i++) { result[2 * i] = list1[i]; // put list1[i] into the result result[2 * i + 1] = list2[i]; // put list2[i] into the result } return result; } }