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 21 //POINT B: Create an ArrayList of just the first names of the names 22 //ArrayList. Use a for loop to print out the names, separated 23 //by spaces. 24 List<String> firstNamesList = firstNames(names); 25 System.out.print("First names: "); 26 for (String firstName : firstNamesList) { 27 System.out.print(firstName + " "); 28 } 29 System.out.println(); 30 31 //POINT C: Use String's join function to create and print a String of 32 //just the first names of the names ArrayList with each 33 //name separated by a comma. 34 String firstNames = String.join(", ", firstNamesList); 35 System.out.println("First names : " + firstNames); 36 37 //POINT D: By analogy from points B and C, print a comma-separated 38 //list of the last names in the names ArrayList. 39 List<String> lastNamesList = lastNames(names); 40 System.out.print("Last names: "); 41 for (String lastName : lastNamesList) { 42 System.out.print(lastName + ", "); 43 } 44 System.out.println(); 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> hyphenatedLastNamesList = hyphenatedNames(lastNames(names)); 55 String hyphenatedLastNames = String.join(", ", hyphenatedLastNamesList); 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 totalFirstLength = totalFirstLength(firstNamesList); 66 System.out.println("Total first name length: " + totalFirstLength); 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 number = 1; 71 for (int i = 0; i < firstNamesList.size(); i = i + 1){ 72 number = number * firstNamesList.get(i).length(); 73 } 74 System.out.println("Product of the length of all first names: " + number); 75 } 76 77 private static String firstName(String directoryStyleName) { 78 int first = directoryStyleName.indexOf(" "); 79 String f = directoryStyleName.substring(first + 1); 80 return f; 81 } 82 83 private static List<String> firstNames(List<String> names) { 84 List<String> firsts = new ArrayList<>(); 85 for (String name: names) { 86 firsts.add(firstName(name)); 87 } 88 return firsts; 89 } 90 91 private static String lastName(String directoryStyleName) { 92 int last = directoryStyleName.indexOf(","); 93 String l = directoryStyleName.substring(0,last); 94 return l; 95 } 96 97 private static List<String> lastNames(List<String> names) { 98 List<String> lasts = new ArrayList<>(); 99 for (String name: names) { 100 lasts.add(lastName(name)); 101 } 102 return lasts; 103 } 104 105 public static List<String> upperCaseNames(List<String> names){ 106 List<String> uppercases = new ArrayList<>(); 107 for (String name : names) { 108 uppercases.add(name.toUpperCase()); 109 } 110 return uppercases; 111 } 112 113 public static List<String> hyphenatedNames(List<String> names){ 114 List<String> hyphenateds = new ArrayList<>(); 115 for (String name : names){ 116 if (name.contains("-")){ 117 hyphenateds.add(name); 118 } 119 } 120 return hyphenateds; 121 } 122 123 public static int totalNameLength(List<String> names) { 124 int totalLength = 0; 125 for (String name : names){ 126 totalLength = totalLength + name.length(); 127 } 128 return totalLength; 129 } 130 131 public static int totalFirstLength(List<String> firstNamesList) { 132 int totalFirstLength = 0; 133 for (String name : firstNamesList){ 134 totalFirstLength = totalFirstLength + name.length(); 135 } 136 return totalFirstLength; 137 } 138 } 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183