1 /* 2 * A program to perform some basic operations on a list of String names. 3 */ 4 5 package arraylists; 6 7 import java.util.ArrayList; 8 import java.util.List; 9 10 public class ArrayListProcessing { 11 12 public static void main(String[] args) { 13 // POINT A: Add some strings which represent names to an ArrayList. 14 List<String> names = new ArrayList<>(); 15 names.add("Holiday, Billie"); 16 names.add("Claudel, Camille"); 17 names.add("Picasso, Pablo"); 18 names.add("Gallen-Kallela, Akseli"); 19 names.add("Zotto, Osvaldo"); 20 names.add("Oshie, TJ"); 21 names.add("Carlson, John"); 22 names.add("Ovechkin, Alex"); 23 24 // POINT B: Create an ArrayList of just the first names of the names 25 // ArrayList. Use a for loop to print out the names, separated 26 // by spaces. 27 List<String> firstNamesList = firstNames(names); 28 System.out.print("First names: "); 29 for (String firstName : firstNamesList) { 30 System.out.print(firstName + " "); 31 } 32 System.out.println(); 33 34 // POINT C: Use String's join function to create and print a String of 35 // just the first names of the names ArrayList with each 36 // name separated by a comma 37 String firstNames = String.join(", ", firstNamesList); 38 System.out.println("First names: " + firstNames); 39 40 // POINT D: By analogy from points B and C, print a comma-separated 41 // list of the last names in the names ArrayList. 42 List<String> lastNamesList = lastNames(names); 43 String lastNames = String.join(", ", lastNamesList); 44 System.out.println("Last names: " + lastNames); 45 46 // POINT E: Print a comma-separated list of all uppercase first names 47 // from the list of names in the names ArrayList. 48 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 49 String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList); 50 System.out.println("Uppercase first names: " + upperCaseFirstNames); 51 52 // POINT F: Print a comma-separated list of all hyphenated last names 53 // from the list of names in the names ArrayList. 54 List<String> hyphenatedNamesList = hyphenatedNames(lastNames(names)); 55 String hyphenatedLastNames = String.join(", ", hyphenatedNamesList); 56 System.out.println("Hyphenated last names: " + hyphenatedLastNames); 57 58 // POINT G: Print the integer value of the total length of all names 59 // in the names ArrayList. 60 int totalLength = totalNameLength(names); 61 System.out.println("Total length: " + totalLength); 62 63 // POINT H: Print the integer value of the total length of all 64 // first names in the names ArrayList. 65 int firstNameLength = totalNameLength(firstNamesList); 66 System.out.println("First name length: " + firstNameLength); 67 68 // POINT I: Print the integer value of the product of the length of 69 // all first names in the names ArrayList. 70 int productOfNameLengths = productOfNameLengths(names); 71 System.out.println("Product of name lengths: " + productOfNameLengths); 72 } 73 74 private static String firstName(String directoryStyleName) { 75 int comma = directoryStyleName.indexOf(","); 76 String first = directoryStyleName.substring(comma + 2); 77 return first; 78 } 79 80 private static String lastName(String directoryStyleName) { 81 int comma = directoryStyleName.indexOf(","); 82 String last = directoryStyleName.substring(0, comma); 83 return last; 84 } 85 86 private static List<String> firstNames(List<String> names) { 87 List<String> firsts = new ArrayList<>(); 88 for (String name : names) { 89 firsts.add(firstName(name)); 90 } 91 return firsts; 92 } 93 94 private static List<String> lastNames(List<String> names) { 95 List<String> lasts = new ArrayList<>(); 96 for (String name : names) { 97 lasts.add(lastName(name)); 98 } 99 return lasts; 100 } 101 102 public static List<String> upperCaseNames(List<String> names) { 103 List<String> uppercases = new ArrayList<>(); 104 for (String name : names) { 105 uppercases.add(name.toUpperCase()); 106 } 107 return uppercases; 108 } 109 110 public static List<String> hyphenatedNames(List<String> names) { 111 List<String> hyphenateds = new ArrayList<>(); 112 for (String name : names) { 113 if (name.contains("-")) { 114 hyphenateds.add(name); 115 } 116 } 117 return hyphenateds; 118 } 119 120 public static int totalNameLength(List<String> names) { 121 int totalLength = 0; 122 for (String name : names) { 123 totalLength = totalLength + name.length(); 124 } 125 return totalLength; 126 } 127 128 public static int productOfNameLengths(List<String> names) { 129 int productLength = 1; 130 for (String name : names) { 131 productLength = productLength * name.length(); 132 } 133 return productLength; 134 } 135 } 136