1 package arraylists; 2 import java.util.ArrayList; 3 import java.util.List; 4 public class ArrayListProcessing { 5 public static void main(String[] args) { 6 // POINT A: Add some strings which represent names to an ArrayList. 7 List<String> names = new ArrayList<>(); 8 names.add("Holiday, Billie"); 9 names.add("Claudel, Camille"); 10 names.add("Picasso, Pablo"); 11 names.add("Gallen-Kallela, Akseli"); 12 names.add("Zotto, Osvaldo"); 13 names.add("VerHeecke, Zachary"); 14 // POINT B: Create an ArrayList of just the first names of the names 15 // ArrayList. Use a for loop to print out the names, separated 16 // by spaces. 17 List<String> firstNamesList = firstNames(names); 18 System.out.print("First names: "); 19 for (String firstName : firstNamesList) 20 { 21 System.out.print(firstName + " "); 22 } 23 System.out.println(); 24 25 // POINT C: Use String’s join function to create and print a String of 26 // just the first names of the names ArrayList with each 27 // name separated by a comma. 28 String firstNames = String.join(" ", firstNamesList); 29 30 System.out.println("First names: " + firstNames); 31 // POINT D: By analogy from points B and C, print a comma-separated 32 // list of the last names in the names ArrayList. 33 List<String> lastNamesList = lastNames(names); 34 System.out.print("last names: "); 35 for (String lastName : lastNamesList) 36 { 37 System.out.print(lastName + " "); 38 } 39 System.out.println(); 40 41 String lastNames = String.join(", ", lastNamesList); 42 43 System.out.println("last names: " + lastNames); 44 // POINT E: Print a comma-separated list of all uppercase first names 45 // from the list of names in the names ArrayList. 46 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 47 48 String upperCaseFirstNames = String.join(" ", upperCaseFirstNamesList); 49 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 56 String hyphenatedLastNames = String.join(" ", hyphenatedLastNamesList); 57 58 System.out.println("hyphenated last names: " + hyphenatedLastNames); 59 // POINT G: Print the integer value of the total length of all names 60 // in the names ArrayList. 61 int totalLength = totalNameLength(names); 62 63 System.out.println("Total length: " + totalLength); 64 // POINT H: Print the integer value of the total length of all 65 // first names in the names ArrayList. 66 int firstLength = firstNameLength(firstNamesList); 67 68 System.out.println("first length: " + firstLength); 69 // POINT I: Print the integer value of the product of the length of 70 // all first names in the names ArrayList. 71 int lastLength = lastNameLength(lastNamesList); 72 73 System.out.println("last length: " + lastLength); 74 } 75 76 private static String firstName(String directorStyleName) { 77 int length =directorStyleName.length(); 78 int comma=directorStyleName.indexOf(","); 79 directorStyleName=directorStyleName.substring(0,comma+1); 80 return directorStyleName; 81 } 82 private static String lastName(String directorStyleName) { 83 int length =directorStyleName.length(); 84 int comma=directorStyleName.indexOf(","); 85 comma=comma+1; 86 directorStyleName=directorStyleName.substring(comma,length); 87 88 return directorStyleName; 89 } 90 private static List<String> firstNames(List<String> names) { 91 List<String> firsts = new ArrayList<>(); 92 for (String name : names) { 93 firsts.add(firstName(name)); 94 } 95 return firsts; 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 { 107 List<String> uppercases = new ArrayList<>(); 108 for (String name : names) 109 { 110 uppercases.add(name.toUpperCase()); 111 } 112 return uppercases; 113 } 114 public static List<String> hyphenatedNames(List<String> names) 115 { 116 List<String> hyphenateds = new ArrayList<>(); 117 for (String name : names) 118 { 119 if (name.contains("-")) 120 { 121 hyphenateds.add(name); 122 } 123 } 124 return hyphenateds; 125 } 126 public static int totalNameLength(List<String> names) 127 { 128 int totalLength = 0; 129 for (String name : names) 130 { 131 totalLength = totalLength + name.length(); 132 } 133 return totalLength; 134 } 135 public static int firstNameLength(List<String> names) 136 { 137 int totalLength = 0; 138 for (String name : names) 139 { 140 totalLength = totalLength + name.length(); 141 } 142 return totalLength; 143 } 144 public static int lastNameLength(List<String> names) 145 { 146 int totalLength = 0; 147 for (String name : names) 148 { 149 totalLength = totalLength + name.length(); 150 } 151 return totalLength; 152 } 153 } 154 155