// Tyler Rigsby, CSE 142 // Several demos of using arrays import java.util.*; public class ArrayDemos { public static void main(String[] args) { int[] myList = {3, 5, 7}; System.out.println(Arrays.toString(myList)); int[] list2 = {3, 5, 7}; System.out.println("== " + (myList == list2)); // doesn't work System.out.println(".equals " + myList.equals(list2)); // doesn't work System.out.println("Arrays.equals " + Arrays.equals(myList, list2)); // works! double[] values = {4.0, 6.0, 9.5}; int result1 = indexOf(values, 6.0); int result2 = indexOf(values, 2.0); System.out.println("indexOf expected 1, got = " + result1); System.out.println("indexOf expected -1, got = " + result2); System.out.println(Arrays.toString(concatenate(myList, list2))); } // takes a double array and value as parameters and returns // the index of the first occurrence of the value in the list, // or -1 if it doesn't exist. public static int indexOf(double[] list, double val) { for (int i = 0; i < list.length; i++) { if (val == list[i]) { return i; } } return -1; } // returns the result of concatenating two arrays together public static int[] concatenate(int[] listA, int[] listB) { int[] result = new int[listA.length + listB.length]; for (int i = 0; i < listA.length; i++) { result[i] = listA[i]; } for (int i = 0; i < listB.length; i++) { result[listA.length + i] = listB[i]; } return result; } }