// Miya Natsuhara // 08-17-2019 // CSE142 // TA: Grace Hopper // Containing a few example of how to interact with ArrayLists. import java.util.*; public class ArrayListMethods { /* arrays vs. ArrayList ---------------------- String[] a --> ArrayList list a.length --> list.size() a[i] --> list.get(i) a[i] = value --> list.set(i, value) new methods: -------------- list.add(value) --> appends the value to the end of the list list.add(i, value) --> insert the value at this index, shift everything else over list.remove(i) --> remove the value at index i, shift everything over */ public static void main(String[] args) { // creating a new ArrayList to hold Strings ArrayList words = new ArrayList(); // filling the ArrayList with Strings words.add("may"); words.add("the"); words.add("odds"); words.add("ever"); words.add("in"); words.add("my"); words.add("uh...."); words.add("favor"); // NOTE: We don't have to do any "Arrays.toString(...)" nonsense! // In the ArrayList class, there is a nice toString method // overriding the inherited toString method from Object that // prints "garbage." System.out.println(words); // fixing the ArrayList to hold the expected Strings... words.add(3, "be"); // Note that we have to adjust the indexes that we modify after the add call // on line 47 ("my" which we are replacing here was originally at index 5 in // ArrayList after we set it up, but after inserting "be", the rest of the // contents get shifted over and "my" is at index 6). words.set(6, "your"); words.remove(7); // get rid of the "uh...." System.out.println(words); int maxLength = maxLength(words); System.out.println("The list longest String in the list is of length " + maxLength); // NOTE: Because ArrayLists accept something called a *type parameter* which says what // type of thing a given ArrayList holds, it turns out that it must be some Object // so it can't be a primitive like int or double. // To get around this, there are several *wrapper classes* defined that basically // work like this primitive types, but are technically Objects and so they can be // used as type parameters. // int --> Integer // double --> Double // boolean --> Boolean // char --> Character // Read more about this in the slides posted for Fri, 8/16 ArrayList nums = new ArrayList(); nums.add(3); nums.add(8); nums.add(92); nums.add(4); nums.add(2); nums.add(17); nums.add(9); System.out.println("nums = " + nums); minToFront(nums); System.out.println("after minToFront, nums = " + nums); } // Returns the length of the longest String in the given list // ArrayList list: the list whose contents are examined public static int maxLength(ArrayList list) { int max = 0; // This is an example of an ArrayList traversal - it looks very similar to an // array traversal! for (int i = 0; i < list.size(); i++) { String next = list.get(i); if (next.length() > max) { max = next.length(); } } return max; } // Moves the minimum value to the front of the given list, otherwise // preserving the order of the elements // ArrayList list: the list to be modified // NOTE: As objects, ArrayLists also obey reference semantics so even though // this method is void, changes made to the parameter within the method // are still reflected in main public static void minToFront(ArrayList list) { int minIndex = 0; for (int i = 1; i < list.size(); i++) { if (list.get(i) < list.get(minIndex)) { minIndex = i; } } int oldValue = list.remove(minIndex); list.add(0, oldValue); // could also have been written in one line: // list.add(0, list.remove(minIndex)); } }