// 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 ArraySample4 { 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, 2, 408}; System.out.println("list1 = " + Arrays.toString(list1)); System.out.println("list2 = " + Arrays.toString(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)); System.out.println("isUnique(list1) returns " + isUnique(list1)); System.out.println("isUnique(list2) returns " + isUnique(list2)); int[] sum = runningSum(list1); System.out.println("runningSum(list1) = " + Arrays.toString(sum)); sum = runningSum(list2); System.out.println("runningSum(list2) = " + 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)); } } // 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 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. public static boolean isUnique(int[] list) { for (int i = 0; i < list.length; i++) { for (int j = i + 1; j < list.length; j++) { if (list[i] == list[j]) { return false; } } } return true; } // 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 a nonempty 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 a nonempty 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; } }