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("Newell, Rachel"); 21 names.add("Washington, George"); 22 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 29 System.out.print("First names: "); 30 for (String firstName : firstNamesList){ 31 System.out.print(firstName + " "); 32 } 33 System.out.println(); 34 35 //POINT C: Use String's join function to create and print a String of 36 // just the first names of the names ArrayList with each 37 // name separated by a comma. 38 39 String firstNames = String.join(", " ,firstNamesList); 40 41 System.out.println("First names: " + firstNames); 42 43 //POINT D: By analogy from points B and C, print a comma-separated 44 // list of the last names in the names ArrayList. 45 List<String> lastNamesList = lastNames(names); 46 47 String lastNames = String.join(", " ,lastNamesList); 48 49 System.out.println("Last names: " + lastNames); 50 51 //POINT E: Print a comma-separated list of all uppercase first names 52 // from the list of names in the names ArrayList. 53 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 54 String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList); 55 56 System.out.println("Uppercase first names: " + upperCaseFirstNames); 57 58 //POINT F: Print a comma-separated list of all hyphenated last names 59 // from the list of names in the names ArrayList. 60 List<String> hyphenatedLastNamesList = hyphenatedNames(lastNames(names)); 61 String hyphenatedLastNames = String.join(", ", hyphenatedLastNamesList); 62 63 System.out.println("Hyphenated last names: " + hyphenatedLastNames); 64 65 //POINT G: Print the integer value of the total length of all names 66 // in the names ArrayList. 67 int totalLength = totalNameLength(names); 68 69 System.out.println("Total length: " + totalLength); 70 71 //POINT H: Print the integer value of the total length of all 72 // first names in the names ArrayList. 73 int totalFirstNameLength = 0; 74 for (String name : firstNamesList) { 75 totalFirstNameLength = totalFirstNameLength + name.length(); 76 } 77 78 System.out.println("Total first name length: " + totalFirstNameLength); 79 80 //POINT I: Print the integer value of the product of the length of 81 // all first names in the names ArrayList. 82 int totalProduct = totalLengthProduct(firstNames(names)); 83 84 System.out.println("Total Product: " + totalProduct); 85 } 86 87 public static int totalLengthProduct(List<String> firstNames) { 88 int totalProduct = 1; 89 for (String name : firstNames) { 90 totalProduct = totalProduct * name.length(); 91 } 92 return totalProduct; 93 } 94 95 public static int totalNameLength(List<String> firstName) { 96 int totalLength = 0; 97 for (String name : firstName) { 98 totalLength = totalLength + name.length(); 99 } 100 return totalLength; 101 } 102 103 public static List<String> hyphenatedNames(List<String> names) { 104 List<String> hyphenateds = new ArrayList<>(); 105 for (String name : names){ 106 if (name.contains("-")){ 107 hyphenateds.add(name); 108 } 109 } 110 return hyphenateds; 111 } 112 113 public static List<String> upperCaseNames(List<String> names) { 114 List<String> uppercases = new ArrayList<>(); 115 for (String name : names) { 116 uppercases.add(name.toUpperCase()); 117 } 118 return uppercases; 119 } 120 private static String firstName(String directoryStyleName) { 121 int spaceLocation = directoryStyleName.indexOf(" "); 122 return directoryStyleName.substring(spaceLocation + 1); 123 } 124 125 private static List<String> firstNames(List<String> names) { 126 List<String> firsts = new ArrayList<>(); 127 for (String name : names) { 128 firsts.add(firstName(name)); 129 } 130 return firsts; 131 } 132 133 private static String lastName(String directoryStyleName) { 134 int commaLocation = directoryStyleName.indexOf(","); 135 return directoryStyleName.substring(0,commaLocation); 136 } 137 138 private static List<String> lastNames(List<String> names) { 139 List<String> lasts = new ArrayList<>(); 140 for (String name : names) { 141 lasts.add(lastName(name)); 142 } 143 return lasts; 144 } 145 146 147 } 148