// Program showing some additional array practice problems. // Each problem is a method, // and main contains sample calls to those methods to demonstrate // their behavior. import java.util.*; public class ArrayPractice { public static void main(String[] args) { int a = 5; int b = 7; System.out.println(a + " " + b); swap(a, b); System.out.println(a + " " + b); System.out.println(); int[] nums = {1, 2, 3, 4, 5}; System.out.println(Arrays.toString(nums)); modify(nums); System.out.println(Arrays.toString(nums)); System.out.println(); double[] vals = {-10.2, 3.5, 9.7, 12.5, 1.3, -0.4, 11.1, 10.0}; System.out.println(Arrays.toString(vals)); cap(vals, 0, 10); System.out.println(Arrays.toString(vals)); reverse(vals); System.out.println(Arrays.toString(vals)); System.out.println(); System.out.println(Arrays.toString(nums)); rotateLeft(nums); System.out.println(Arrays.toString(nums)); System.out.println(); int[] nums2 = {4, 8, 15, 16, 23, 42}; System.out.println(Arrays.toString(nums) + " / " + Arrays.toString(nums2) + " -> "); System.out.println(Arrays.toString(interleave(nums, nums2))); } // an example of an array mystery problem--there will a problem like this // on the final. public static void mystery() { int[] a = {1, 7, 5, 6, 4, 14, 11}; for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } } System.out.println("mystery array = " + Arrays.toString(a)); } // We saw that this is correct code for swapping two int values, but it // only swaps the local copies of these values and has no effect on any // variables that are used in calling the method. public static void swap(int x, int y) { System.out.println("Before swap: " + x + " " + y); int temp = x; x = y; y = temp; System.out.println("After swap: " + x + " " + y); } // We saw that this method was able to change the array contents because // of reference semantics. public static void modify(int[] list) { list[0] = -724; list[1] = -425; } // a method to limit all values in an array to within a given range public static void cap(double[] nums, double min, double max) { for (int i = 0; i < nums.length; i++) { if (nums[i] > max) { nums[i] = max; } else if (nums[i] < min) { nums[i] = min; } // nums[i] = Math.min(nums[i], max); // nums[i] = Math.max(nums[i], min); } } // this method reverses the sequence of values in an array public static void reverse(double[] list) { for (int i = 0; i < list.length / 2; i++) { double first = list[i]; double last = list[(list.length - 1) - i]; list[i] = last; list[(list.length - 1) - i] = first; } } // a method to reverse a String and return the result of the reversal public static String reverse(String str) { String result = ""; for (int i = str.length() - 1; i >= 0; i--) { result += str.charAt(i); } 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 > 0) { int temp = list[0]; for (int i = 1; i < list.length; i++) { list[i - 1] = list[i]; } list[list.length - 1] = 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. public static int[] interleave(int[] list1, int[] list2) { int min = Math.min(list1.length, list2.length); int[] result = new int[min * 2]; for (int i = 0; i < result.length; i++) { if (i % 2 == 0) { // result[0] = list1[0] // result[2] = list1[1] // result[4] = list1[2] // ... result[i] = list1[i / 2]; } else { // result[1] = list2[0] // result[3] = list2[1] // result[5] = list2[2] // ... result[i] = list2[i / 2]; } } return result; } // alternate interleave, iterating over smaller list, not result public static int[] alternateInterleave(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 // result[0] = list1[0] // result[1] = list2[0] // result[2] = list1[1] // result[3] = list2[1] // ... } return result; } }