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