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 13 // 14 public static void main(String[] args) { 15 // 16 // // Point A: Add some strings which represent names to an ArrayList. 17 List<String> names = new ArrayList<>(); 18 names.add("Holiday, Billie"); 19 names.add("Claudel, Camille"); 20 names.add("Picasso, Pablo"); 21 names.add("Gallen-Kallela, Akseli"); 22 names.add("Zotto, Osvaldo"); 23 24 names.add("Rock, Johnson"); 25 names.add("McCoy, James"); 26 names.add("Gates, Bill"); 27 28 // POINT B: 29 // Create an ArrayList of just the first names of the names 30 // ArrayList. Use a For loop to print out the names, separated by spaces. 31 32 List <String> firstNamesList = firstNames(names); 33 34 System.out.println("First names: "); 35 for ( String firstName : firstNamesList ) { 36 System.out.println(firstName + " "); 37 } 38 System.out.println(); 39 40 41 // POINT C: Use String's join function to create and print a String of just the first names of the names ArrayList with each name 42 // separated by a comma. 43 44 String firstNames = String.join(", ", firstNamesList); 45 46 System.out.println("First names: " + firstNames); 47 48 // POINT D: By analogy from Points B and C, print a comma-separated list of the last names in the names ArrayList. 49 50 // LAST NAME 51 System.out.println(); 52 53 List <String> lastNamesList = lastNames(names); 54 System.out.print("Last names: "); 55 56 String lastNames = String.join(", ", lastNamesList); 57 System.out.println(lastNames); 58 59 System.out.println(); 60 61 // 62 63 // POINT E: Print a comma-separated list of all uppercase first names from the list of the last names in the names ArrayList. 64 List<String> upperCaseFirstNamesList = upperCaseNames(firstNames(names)); 65 String upperCaseFirstNames = String.join(", ", upperCaseFirstNamesList); 66 67 System.out.println("Uppercase first names: " + upperCaseFirstNames); 68 69 // POINT F: Print a comma-separated list of all hyphenated last names from the list of names in the names ArrayList. 70 71 List <String> hyphenatedNames = hyphenatedNames(names); 72 List <String> hyphenatedLastNames = lastNames(hyphenatedNames); 73 System.out.print("Hyphenated last names: "); 74 75 String hyp = String.join(", ", hyphenatedLastNames); 76 System.out.println(hyp); 77 78 System.out.println(); 79 80 81 82 83 // POINT G: Print the Integer value of the total length of all names in the names Arraylist. 84 85 // POINT H: Print the Integer value of the total length of all first names in the names Arraylist. 86 87 // POINT I: Print the Integer value of the product of the length of all first names in the names Arraylist. 88 89 } // END MAIN 90 91 private static String firstName (String directoryStyleName) 92 { 93 int spaceLoc = directoryStyleName.indexOf(" "); 94 return directoryStyleName.substring(spaceLoc + 1); 95 } 96 97 private static List<String> firstNames (List<String> names) { 98 List<String> firsts = new ArrayList<>(); 99 for (String name : names) { 100 firsts.add(firstName(name)); 101 } 102 return firsts; 103 } 104 105 private static List<String> lastNames (List<String> names) { 106 List<String> lasts = new ArrayList<>(); 107 for (String name : names) { 108 lasts.add(lastNames(name)); 109 } 110 return lasts; 111 } 112 113 private static String lastNames(String directoryStyleName) { 114 int lastNameComaLoc = directoryStyleName.indexOf(","); 115 return directoryStyleName.substring(0, lastNameComaLoc); 116 } 117 118 // CONVERT TO UPPERCASE 119 public static List<String> upperCaseNames(List<String> names) { 120 List <String> uppercases = new ArrayList<>(); 121 for ( String name : names ) { 122 uppercases.add(name.toUpperCase()); 123 } 124 return uppercases; 125 } 126 public static List<String> hyphenatedNames(List<String> names) { 127 List<String> hyphenateds = new ArrayList<>(); 128 for (String name : names) { 129 if (name.contains("-")) { 130 hyphenateds.add(name); 131 } 132 } 133 return hyphenateds; 134 } 135 136 } 137