// Kyle Thayer, CSE 142 // Some examples of using ArrayLists import java.util.*; // Translation from array to ArrayList: // String[] ArrayList // new String[n] new ArrayList() // int[] ArrayList // list[i] list.get(i) // list[i] = val list.set(i, val); // list.length list.size() // New operations: // list.add(val) appends val to list // list.add(i, val) inserts val at index i shifting subsequent values to the right // list.remove(i) removes and returns the value at i // list.clear() clears the entire ArrayList public class ArrayListExamples { public static void main(String[] args) { // create an ArrayList of Strings. It starts out empty ArrayList list = new ArrayList(); // add a bunch of words to the list list.add("it's"); list.add("friday"); list.add("friday"); list.add("gotta"); list.add("get"); list.add("down"); list.add("on"); list.add("thursday"); //if we are just replacing a value, this is the easiest way // to do so: list.set(7, "friday"); //we can alternately remove the old value and add the new one: // list.remove(7); // list.add("friday"); list.remove(3); list.add(3, "got to"); // find the length of the longest string System.out.println("length of the longest is " + maxLength(list)); //To create an ArrayList of ints, we need to use the wrapper // class "Integer" ArrayList numbers = new ArrayList(); numbers.add(42); numbers.add(17); numbers.add(99); numbers.add(-2); // move the smallest number to the front minToFront(numbers); System.out.println("numbers = " + numbers); // remove all strings that have an even length removeEvenLength(list); System.out.println("list = " + list); list.add("down"); list.add("on"); list.add("friday"); // double each element in the list stretch(list); System.out.println("list = " + list); } // Returns the length of the longest String in the given list public static int maxLength(ArrayList list) { int longestLen = 0; // longest length found so far, starts at 0 // loop over the ArrayList for(int i = 0; i < list.size(); i++){ // get the i'th element of the list String thisString = list.get(i); // if it is longer than the previous longest length, update the longest length if(thisString.length() > longestLen){ longestLen = thisString.length(); } } return longestLen; } // Moves the minimum value to the front of the given list, otherwise // preserving the order of the elements public static void minToFront(ArrayList list) { // find the index where the minimum element is located int smallestIndex = 0; // start out assuming it's index 0 // see if any other element in the list is smaller for(int i = 1; i < list.size(); i++){ int currentNum = list.get(i); // if the current number is smaller, update the smallestIndex if(currentNum < list.get(smallestIndex)){ smallestIndex = i; } } // move it to the front // NOTE: This code doesn't handle the case where the list // is of length 0 int temp = list.remove(smallestIndex); list.add(0, temp); } // Removes from the list all strings of even length public static void removeEvenLength(ArrayList list) { // go over the list to find and remove even strings for(int i = 0; i < list.size(); i++){ // get the current string at index i String current = list.get(i); // see if the current string has an even length if(current.length() % 2 == 0){ // remove the string list.remove(i); // since removing the string shifted all the indexes after it // by -1, do i-- to keep i lined up with the list how we want i--; } } } // Duplicates every value in the list // Note: We didn't do this in class but this method can be a bit tricky too! public static void stretch(ArrayList list) { // Make sure we do i+= 2 since we are adding one to the list each time, // otherwise you'll get an infinite loop (Try running it through the debugger) for (int i = 0; i < list.size(); i += 2) { // get the current string String val = list.get(i); // add the current string to the current location, so there are // two of them there. This adds one the the indexes of everything after it. list.add(i, val); } // Alternate solution: Go over the list backward, so we never look back at the // part of the list where the indexes changed /* for(int i = list.size() - 1; i >= 0; i--){ String val = list.get(i); list.add(i, val); } */ } }