Rewrite lines 1025 in Fig. 16.3 to be more concise by using the asList method and the
Question:
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
Transcribed Image Text:
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
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (1 review)
To make the code more concise we can utilize the ArraysasList method which creates a fixedsize list ...View the full answer
Answered By
Raunak Agarwal
Teaching is my hobby and now my profession. I teach students of CA and CFA(USA) in batches of 100 students and have a 5 year experience.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
We will continue working on Python. Using a Google Colab notebook write the python statement to define a Pizza class. The test harness is provided to you by the instructor as well as the resulting...
-
Choose a topic that can be applied to one of the following economic concepts. (Possible curves: Lorenz curve, marginal product of labor curve, labor demand and supply curves, utility function and...
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-5. Ivan's grandfather died and left a portfolio of municipal bonds. In 2012, they pay Ivan...
-
Last year, Intrepid Corporations tax return revealed the following items: dividends from 20 percent owned domestic corporations - $60,000; Gross income from services rendered - $300,000;...
-
Three expense recognition methods (associating cause and effect, systematic and rational allocation, and immediate recognition) were discussed in the text under the matching principle. Indicate the...
-
On April 1, 2017, Bullen Company transferred machinery used in its business to Eaton Inc. in exchange for Eaton common stock. Both Bullen and Eaton use the calendar year for tax purposes. Bullen's...
-
Determine the sidelighting workplace illuminances for a \(20 \mathrm{ft}\) long, \(15 \mathrm{ft}\) wide (deep), \(8 \mathrm{ft}\) high light-colored room with a \(15 \mathrm{ft}\) long by \(5...
-
Boulware Products, Inc. produces printers for wholesale distributors. It has just completed packaging an order from Shawl Company for 450 printers. Before the order is shipped, the controller wants...
-
Consider the equation and the relation "( x, y ) R (0, 2)", where R is read as "has distance 1 of". For example, "(0, 3) R (0, 2)", that is, "(0, 3) has distance 1 of (0, 2)". This relation can also...
-
Write a program that reads in a series of first names and eliminates duplicates by storing them in a Set. Allow the user to search for a first name.
-
Determine whether each of the following statements is true or false. If false, explain why. a) Vectors are synchronized by default. b) Class Stack does not have a method add. c) In order to use the...
-
What is the purpose of closing entries?
-
In view of significant financial losses to investors, corporate fraud does not really affect the U.S. economy and investor confidence. True/false
-
In what ways do you think development banks have supported the growth of green finance?
-
The PCAOB ended the self-regulation of the external auditing profession. True/false
-
Auditors should not demand full access to the entire general ledger so that they can be more focused on the audit. True/false
-
In what ways do you think the activities of central banks could affect the natural environment?
-
A flat leaf spring has fluctuating stress of max = 360 MPa and min = 160 MPa applied for 8 (104) cycles. If the load changes to max = 320 MPa and min = 200 MPa, how many cycles should the spring...
-
Find the reduced echelon form of each of the matrices given in Problems 120. c 1 26 + 4
-
Compare and contrast HDLC with PPP.
-
Compare the flag byte and the escape byte in PPP. Are they are the same? Explain.
-
In Figure 11.20, explain why we need only one address field. Explain why the address is set to the predefined value of (11111111) 2. Figure 11.20 (11111111)2 (00000011)2 Flag Address Control Protocol...
-
2.1 Show the effect of the following transactions in the accounting equation. (15) For example: No. Account DR Account CR 1. Furniture Capital A OE + L + 20 000 +20 000 0 1. The owner deposited R 60...
-
a bil b ci A 2 0.8 0.9 2.0 Security B C 3 1 1.1 0.9 1.3 1.1 1.0 1.5 = 2.0,12 = 2.5, calculate Assuming Is are uncorrelated and = 8, = 4, the following using the general multi-index model: (1)...
-
8. Assume a random sample of three paired observations on two variables (X and Y) provided the following data. X Y 326 11 16 12 (10) a. Manually calculate the variance and standard deviation of X and...
Study smarter with the SolutionInn App