Question: Rewrite lines 1025 in Fig. 16.3 to be more concise by using the asList method and the LinkedList constructor that takes a Collection argument. Fig.
Rewrite lines 10–25 in Fig. 16.3 to be more concise by using the asList method and the LinkedList constructor that takes a Collection argument.
Fig. 16.3
1 // Fig. 16.3: ListTest.java // Lists, LinkedLists and ListIterators. 3 import java.util.List; 4 import java.util.LinkedList; 5 import java.util.ListIterator; 6 7 8 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 public class ListTest { public static void main(String[] args) { 72 73 74 75 76 77 78 } // add colors elements to list1 String[] colors= {"black", "yellow", "green", "blue", "violet", "silver"}; List list1= new LinkedList (); for (String color colors) { list1.add(color); } // add colors2 elements to list2 String[] colors2 = } {"gold", "white", "brown", "blue", "gray", "silver"}; List list2 = new LinkedList (); for (String color colors2) { list2.add(color); } list1.addAll(list2);// concatenate lists list2 = null; // release resources. printList (list1); // print listl elements convertToUppercaseStrings (list1); // convert to uppercase string printList (list1); // print list1 elements System.out.printf("%nDeleting elements 4 to 6..."); removeItems (list1, 4, 7); // remove items 4-6 from list printList (list1); // print list1 elements printReversedList (list1); // print list in reverse order } // output List contents private static void printList (List list) { System.out.printf("%nlist: %n"); for (String color : list) { System.out.printf( "%s", color); } System.out.println(); } // locate String objects and convert to uppercase private static void convertToUppercaseStrings (List list) { ListIterator iterator = list.listIterator(); while (iterator.hasNext()) { String color = iterator.next(); // get item iterator.set(color.toUpperCase()); // convert to upper case } } // obtain sublist and use clear method to delete sublist items private static void removeItems (List list, int start, int end) { list.subList(start, end).clear(); // remove items } // print reversed list private static void printReversed List (List list) { ListIterator iterator = list.listIterator(list.size()); System.out.printf("%nReversed List:%n"); // print list in reverse order while (iterator.hasPrevious()) { System.out.printf("%s ", iterator.previous()); } list: black yellow green blue violet silver gold white brown blue gray silver list: BLACK YELLOW GREEN BLUE VIOLET SILVER GOLD WHITE BROWN BLUE GRAY SILVER Deleting elements 4 to 6... list: BLACK YELLOW GREEN BLUE WHITE BROWN BLUE GRAY SILVER Reversed List: SILVER GRAY BLUE BROWN WHITE BLUE GREEN YELLOW BLACK
Step by Step Solution
3.46 Rating (162 Votes )
There are 3 Steps involved in it
To make the code more concise we can utilize the ArraysasList method which creates a fixedsize list ... View full answer
Get step-by-step solutions from verified subject matter experts
