// 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 = {12, 8, 4, 0, 208, 14, 42, 38, 72, 96, 102}; int[] list2 = {84, 42, 92, 96, 2, 4, 8, 0, 72, 302, 17, 408}; System.out.println("list1 = " + Arrays.toString(list1)); System.out.println("list2 = " + Arrays.toString(list2)); System.out.println("allEven(list1) returns " + allEven(list1)); System.out.println("allEven(list2) returns " + allEven(list2)); System.out.println("indexOf(list1, 42) returns " + indexOf(list1, 42)); System.out.println("indexOf(list1, 16) returns " + indexOf(list1, 16)); System.out.println("indexOf(list2, 42) returns " + indexOf(list2, 42)); System.out.println("indexOf(list2, 16) returns " + indexOf(list2, 16)); int[] sum = runningSum(list1); System.out.println("runningSum(list1) returns " + Arrays.toString(sum)); sum = runningSum(list2); System.out.println("runningSum(list2) returns " + Arrays.toString(sum)); 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)); } System.out.println("result of rotating right:"); System.out.println(" list2 = " + Arrays.toString(list2)); for (int i = 0; i < list2.length; i++) { rotateRight(list2); System.out.println(" list2 = " + Arrays.toString(list2)); } System.out.println("list1 before sort = " + Arrays.toString(list1)); gnomeSort(list1); System.out.println("list1 after sort = " + Arrays.toString(list1)); } // Write a static method called allEven that takes an array of integers as // a parameter and that returns true if all the integers are even and that // returns false otherwise public static boolean allEven(int[] list) { for (int i = 0; i < list.length; i++) { if (list[i] % 2 != 0) { return false; } } return true; } // Write a static method called indexOf that takes an array of integers and // a value as parameters and that returns the index of the first occurrence // of the value in the array, returning -1 if not found public static int indexOf(int[] list, int target) { for (int i = 0; i < list.length; i++) { if (list[i] == target) { return i; } } return -1; } // Write a static method called runningSum that takes an array of integers // as a parameter and that returns a new array that contains the running // sum of the numbers in the array (where the i-th value of the new array // is the sum of the first i values of the original array) public static int[] runningSum(int[] list) { int[] result = new int[list.length]; if (list.length > 0) { result[0] = list[0]; for (int i = 1; i < list.length; i++) { result[i] = list[i] + result[i - 1]; } } 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) { int temp = list[0]; for (int i = 0; i < list.length - 1; i++) { list[i] = list[i + 1]; } list[list.length - 1] = temp; } // 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; } // In the 11:30 lecture, the following method was discussed. It sorts an // array of ints. public static void gnomeSort(int[] list) { int i = 0; while (i < list.length - 1) { if (list[i] <= list[i + 1]) { i++; } else { int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; if (i > 0) { i--; } } } } }