// Marty Stepp, CSE 143, Winter 2010 // This program demonstrates methods that process ArrayLists as parameters. import java.util.*; public class Stars { // Tests the methods we wrote in class below. public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("the"); list.add("quick"); list.add("brown"); list.add("fox"); System.out.println(list); addStars(list); System.out.println(list); removeStars(list); System.out.println(list); } // Adds a "*" string after each element of the given list. // Example: [hi, there, you] becomes [hi, *, there, *, you, *]. // Assumes that the list is not null. public static void addStars(ArrayList list) { for (int i = 0; i < list.size(); i += 2) { list.add(i + 1, "*"); } } // Removes all elements at odd indexes from the given list, // assuming that they are star * marks. // Example: [hi, *, there, *, you, *] becomes [hi, there, you]. // Assumes that the list is not null. public static void removeStars(ArrayList list) { // two possible solutions below // for (int i = 1; i < list.size(); i++) { // list.remove(i); // } for (int i = list.size() - 1; i >= 0; i -= 2) { list.remove(i); } } }