// 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)); int[] list4 = {18, 7, 4, 24, 11}; int[] result4 = doubleSize(list4); System.out.println("doubleSize(list4) returns " + Arrays.toString(result4)); int[] result1 = interleave(list1, list2); int[] result2 = interleave(list1, list3); System.out.println("interleave(list1, list2) ="); System.out.println(Arrays.toString(result1)); System.out.println("interleave(list1, list3) ="); System.out.println(Arrays.toString(result2)); System.out.println("result of rotating left:"); System.out.println(" list1 = " + Arrays.toString(list1)); for (int i = 0; i < list1.length; i++) { rotateLeft(list1); System.out.println(" list1 = " + Arrays.toString(list1)); } } // 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++){ // see if we found a pair that isn't sorted if(list[i] > list[i+1]){ return false; // we found a problem in the sorting, so return false } } // no problems found, so its sorted return true; } // 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) { // find the shortest length and use that (in the case that one list is longer) int shortestLength = Math.min(list1.length, list2.length); // make our new array double the length of the shortest one int[] results = new int[shortestLength * 2]; // go over the indexes of the shorter array for(int i = 0; i < shortestLength; i++){ results[i*2] = list1[i]; results[i*2+1] = list2[i]; } return results; } // Write a static method called doubleSize that takes an // array of integers as an argument and that returns a new array twice as large // as the original that replaces every integer from the original list with a // pair of integers, each half the original. If a number in the original list // is odd, then the first number in the new pair should be one higher than the // second so that the sum equals the original number. For example, if a // variable called list stores this sequence of values: // [18, 7, 4, 24, 11] // The number 18 is split into the pair (9, 9), the number 7 is split into (4, // 3), the number 4 is split into (2, 2), the number 24 is split into (12, 12) // and the number 11 is stretched into (6, 5). Thus, the call: // int[] result = doubleSize(list); // should return an array containing this sequence: // [9, 9, 4, 3, 2, 2, 12, 12, 6, 5] public static int[] doubleSize(int[] list) { // new array double length int[] result = new int[list.length * 2]; // go over the given lists length for(int i = 0; i < list.length; i++){ result[i*2] = (list[i] + 1) / 2; // divide by 2, rounding up (clever math trick) result[i*2+1] = list[i] / 2; // divide by 2 rounding down (normal int division) } return result; } // Write a static method called rotateLeft that takes an array // of integers as a parameter and that rotates all values to the // left by one position, rotating the first value to the back of // the array. For example, given the list {1, 2, 3, 4}, a call // on rotateLeft should yield {2, 3, 4, 1}. public static void rotateLeft(int[] list) { if (list.length > 1) { // make sure there is work to do and it wont break if there isn't // save the first element int temp = list[0]; // move everything on the list one to the left for (int i = 0; i < list.length - 1; i++) { list[i] = list[i + 1]; } // put the first element at the end list[list.length - 1] = temp; } } }