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 names.add("Eugen, Prinz"); 22 names.add("Hipper, Admiral"); 23 names.add("HMS, Amazon"); 24 25 // POINT B: Create an ArrayList of just the first names of the names 26 // ArrayList. Use a for loop to print out the names, separated 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 just the first names 36 // of the names ArrayList with each name separated by 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 list of the last names in the 42 // names ArrayList. 43 List<String> lastNamesList = lastNames(names); 44 45 String lastNames = String.join(", " , lastNamesList); 46 47 System.out.println("Last names: " + lastNames); 48 49 // POINT E: Print a comma-separated list of all uppercase first names 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 last names from the list of names in the names ArrayList. 56 List<String> hyphenatedLastNamesList = hyphenatedNames(lastNames(names)); 57 String hyphenatedLastNames = String.join(", ", hyphenatedLastNamesList); 58 59 System.out.println("Hyphenated last names: " + hyphenatedLastNames); 60 61 // POINT G: Print the integer value of the total length of all names in the names ArrayList. 62 int totalLength = totalNameLength(names); 63 64 System.out.println("Total length: " + totalLength); 65 66 // POINT H: Print the integer value of the total length of all first names in the names ArrayList. 67 int totalFirstLength = totalNameLength(firstNamesList); 68 69 System.out.println("Total first name length: " + totalFirstLength); 70 71 // POINT I: Print the integer value of the product of the length of all first names in the names ArrayList. 72 int productLength = productNameLength(names); 73 74 System.out.println("Product name length: " + productLength); 75 76 } 77 78 public static int productNameLength(List<String> names){ 79 int productLength = 1; 80 for (String name : names){ 81 productLength = productLength * name.length(); 82 } 83 return productLength; 84 } 85 public static int totalNameLength(List<String> names){ 86 int totalLength = 0; 87 for (String name : names){ 88 totalLength = totalLength + name.length(); 89 } 90 return totalLength; 91 } 92 93 public static List<String> hyphenatedNames(List<String> names){ 94 List<String> hyphenated = new ArrayList<>(); 95 for (String name : names){ 96 if (name.contains("-")){ 97 hyphenated.add(name); 98 } 99 } 100 return hyphenated; 101 } 102 103 public static List<String> upperCaseNames(List<String> names){ 104 List<String> uppercases = new ArrayList<>(); 105 for(String name : names){ 106 uppercases.add(name.toUpperCase()); 107 } 108 return uppercases; 109 } 110 111 public static List<String> firstNames(List<String> names){ 112 List<String> firsts = new ArrayList<>(); 113 for (String name : names) { 114 firsts.add(firstName(name)); 115 } 116 return firsts; 117 } 118 119 public static List<String> lastNames(List<String> names){ 120 List<String> lasts = new ArrayList<>(); 121 for (String name : names) { 122 lasts.add(lastName(name)); 123 } 124 return lasts; 125 } 126 127 private static String firstName(String directoryStyleName) { 128 int firstNamePosition = directoryStyleName.indexOf(","); 129 String firstName = directoryStyleName.substring(firstNamePosition + 2, directoryStyleName.length()); 130 return firstName; 131 } 132 133 private static String lastName(String directoryStyleName) { 134 int lastNamePosition = directoryStyleName.indexOf(","); 135 String lastName = directoryStyleName.substring(0, lastNamePosition); 136 return lastName; 137 } 138 } 139