// Several examples of array problems that let you show off your // problem-solving skills. // All include traversals. Examples of creating a new array, modifying // an existing array and calculating a value based on an array. // We discussed the importance of playing with a human's approach to the // problem before writing code and working through examples in our head. // We mentioned looking out for OBOBs (off by one bugs), being careful not to // return too early and making sure we returned the appropriate type. import java.util.*; public class ArrayPractice { public static void main(String[] args) { int[] numbers = {3, 8, 6}; System.out.println(Arrays.toString(delta(numbers))); int[] a1 = {1, 2, 4, 5}; System.out.println(isUnique(a1)); int[] a2 = {1, 2, 4, 5, 5}; System.out.println(isUnique(a2)); String[] a3 = {"a", "b", "c"}; swapPairs(a3); System.out.println(Arrays.toString(a3)); } // Write a static method named delta that accepts an array of integers as a // parameter and returns a new array formed by inserting between each pair of // values the difference between those values. For example, given this array: // int[] numbers = {3, 8, 6}; The call of delta(numbers) should return the following // array (new elements are bolded): {3, 5, 8, -2, 6} // 0 1 2 // {5, 6, 3} // 0 2 4 // {5, 1, 6, -3, 3} // Note: last element is treated differently (no neighbor) -- fencepost public static int[] delta(int[] a) { int[] result = new int[a.length * 2 - 1]; for (int i = 0; i < a.length - 1; i++) { result[i * 2] = a[i]; result[i * 2 + 1] = a[i + 1] - a[i]; } result[result.length - 1] = a[a.length - 1]; return result; } // Write a static method called isUnique that takes an array of integers as // a parameter and that returns true if the values in the list are unique // and that returns false otherwise. The values in the list are considered // unique if there is no pair of values that are equal. // 5 2 6 123 5 3 -- we have to compare every element with every other // when do we know it's not unique? As soon as we find a pair that match // when do we know it IS unique? Only once we've gone through all elements public static boolean isUnique(int[] a) { for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] == a[j]) { return false; } } } return true; } // Write a method named swapPairs that accepts an array of strings as a parameter // and switches the order of values in a pairwise fashion. Your method should switch // the order of the first two values, then switch the order of the next two, switch // the order of the next two, and so on. If there are an odd number of values, the final // element is not moved. // 0 2 // "a" "b" "c" "d" // "b" "a" "d" "c" // "a" "b" "c" -- be sure to test the odd case // "b" "a" // obob: off by one bug public static void swapPairs(String[] a) { for (int i = 0; i < a.length - 1; i += 2) { //a[i] swap with a[i + 1] String temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } public static void swapPairs2(String[] a) { for (int i = 0; i < a.length / 2; i++) { String temp = a[i * 2]; a[i * 2] = a[i * 2 + 1]; a[i * 2 + 1] = temp; } } }